X.509 with GoAn X.509 certificate can be used to provide a public key in a trust worthy way. Click on the blue button to generate a certificate and show its details. |
package main import ( "crypto" "crypto/tls" "crypto/x509/pkix" "fmt" "crypto/rsa" "github.com/InfiniteLoopSpace/go_S-MIME/pki" ) var ( root = pki.New(pki.IsCA, pki.Subject(pkix.Name{ CommonName: "root.example.com", })) intermediate = root.Issue(pki.IsCA, pki.Subject(pkix.Name{ CommonName: "intermediate.example.com", })) leaf = intermediate.Issue(pki.Subject(pkix.Name{ CommonName: "leaf.example.com", })) AlicekeyPair = tls.Certificate{ Certificate: [][]byte{leaf.Certificate.Raw, intermediate.Certificate.Raw, root.Certificate.Raw}, PrivateKey: leaf.PrivateKey.(crypto.PrivateKey), } ) func main() { fmt.Printf("== Certificate details ==\n") fmt.Printf("Version: %d\n", leaf.Certificate.Version) fmt.Printf("Serial Number: %s\n", leaf.Certificate.SerialNumber) fmt.Printf("Subject Key ID: %s\n", leaf.Certificate.SubjectKeyId) fmt.Printf("Subject: %s\n", leaf.Certificate.Subject) fmt.Printf("Date (Not before): %s\n", leaf.Certificate.NotBefore) fmt.Printf("Date (Not after): %s\n", leaf.Certificate.NotAfter) fmt.Printf("\nSignature: %x\n", leaf.Certificate.Signature) fmt.Printf(" Algorithm: %s\n", leaf.Certificate.SignatureAlgorithm) fmt.Printf("\nIssuer: %s\n", leaf.Certificate.Issuer) fmt.Printf(" Common name: %s\n", leaf.Certificate.Issuer.CommonName) fmt.Printf("\nPublic key method: %s\n", leaf.Certificate.PublicKeyAlgorithm) // Certificate has Alice's public key pub := leaf.Certificate.PublicKey.(*rsa.PublicKey) fmt.Printf(" Public Key: e=%d, N=%s\n", pub.E, pub.N) // Keypair has Alice's private key priv := AlicekeyPair.PrivateKey.(*rsa.PrivateKey) fmt.Printf(" Private Key: d=%d, N=%s\n", priv.D, priv.N) }
A sample run is:
== Certificate details == Version: 3 Serial Number: 8055688527075915463 Subject Key ID: Subject: CN=leaf.example.com Date (Not before): 1970-01-01 00:00:00 +0000 UTC Date (Not after): 2032-03-10 11:00:18 +0000 UTC Signature: 57f784f73d435fa099ffd3c3c98b3385bf810f76edd62bd7d7b542e12bce284235ace7e85d565bc59061efcb032e8911e8bc37beedf7145bf02f467143fe189de3a6d77ae891eb6c24bbd5fb1f9caa40f0723823cc4af04532dd1b70faf24793498a7ddd782d8beac3072b3506e29d9181782f6bada93b57f16185bc662062f8d3292b5dea5fd635679631562ae141d911d1368278ec6908a02a4604b4c497a9404ebb61b4d8de8db097856e2d132f539d54a42acfea43ca07ec8b7d6d21b390f3b62bffe8eff474112f32755765186a00ae0958497e42aef247191c1099398bc54128bc5678e7eec6198f55a53b677081adb05c274ac5055c56e6f5f2c69cd3 Algorithm: SHA256-RSA Issuer: CN=intermediate.example.com Common name: intermediate.example.com Public key method: RSA Public Key: e=65537, N=22440040138286940438404846638792705632522317545982571542751844037984157452183886811032545555882190933521248391351760205801266122860169284874330845696750589344841212542405502803500787204882676973381120032623658768414439406467071152577793825246668153280147956778062081554291380441182782248018470783675815694832101309741263690600031977110304483343181900229606070648538842562300770254221207575884700367955292936590540097525052748462331944905637942222630124678777120015060120335860496846326248673822938190035754401584484037154275212869005210412452140314250233856356957864775033977488178501452747476530574216486443244549369 Private Key number of bits: 2048 Private Key: d=11806384546260463452044456062258133312708882483223599636321869085764251233787823689384823890510304493320508503321788358579633751656949465366919478927470849614713365727370556207447863704648665406093571567891364847241989794686834618796022230012517579279686004816582367732327099027914361577336846225672914551680658833980067430569871447564998762973676053679724119827097227122087423820604441676733700341486812017928971973452482064957953736867541562759786360911629019410481037152104921030644470932872036415750284886560383482805747488075684132536459601628579248075436203191898283957656630228679225155506422673850711560333493, N=22440040138286940438404846638792705632522317545982571542751844037984157452183886811032545555882190933521248391351760205801266122860169284874330845696750589344841212542405502803500787204882676973381120032623658768414439406467071152577793825246668153280147956778062081554291380441182782248018470783675815694832101309741263690600031977110304483343181900229606070648538842562300770254221207575884700367955292936590540097525052748462331944905637942222630124678777120015060120335860496846326248673822938190035754401584484037154275212869005210412452140314250233856356957864775033977488178501452747476530574216486443244549369 Private Key: p=158577094180355006190426722421976396953751310926797762325001021856911768888509002654693162120081502286321953284113130697714162426243739196935751621558430080260966283395165985747250695089850902488572198867258349345246076254912464009264347517937173219700715749791585820329316034018637138487475400426125023363231, q=141508710663881481209519441351839941042466078620120277285127932942814579095834962311051279962377142404011183185421590701904576566338566629182707645385186118068324556873549601759744142486033821028623558521643996076182840798538934331555505103606985374341853072106564199821703588719734913420134784386234420554599
If we multiply the two prime numbers together (\(p\) and \(q\)), we get:
>>> p=158577094180355006190426722421976396953751310926797762325001021856911768888509002654693162120081502286321953284113130697714162426243739196935751621558430080260966283395165985747250695089850902488572198867258349345246076254912464009264347517937173219700715749791585820329316034018637138487475400426125023363231 >>> q=141508710663881481209519441351839941042466078620120277285127932942814579095834962311051279962377142404011183185421590701904576566338566629182707645385186118068324556873549601759744142486033821028623558521643996076182840798538934331555505103606985374341853072106564199821703588719734913420134784386234420554599 >>> N=p*q >>> print (N) 22440040138286940438404846638792705632522317545982571542751844037984157452183886811032545555882190933521248391351760205801266122860169284874330845696750589344841212542405502803500787204882676973381120032623658768414439406467071152577793825246668153280147956778062081554291380441182782248018470783675815694832101309741263690600031977110304483343181900229606070648538842562300770254221207575884700367955292936590540097525052748462331944905637942222630124678777120015060120335860496846326248673822938190035754401584484037154275212869005210412452140314250233856356957864775033977488178501452747476530574216486443244549369
and which is equal to the modulus (\(N\)).