55 "crypto/ecdsa"
66 "crypto/elliptic"
77 "crypto/rand"
8+ "crypto/sha256"
89 "crypto/x509"
910 "crypto/x509/pkix"
1011 "encoding/pem"
@@ -17,11 +18,11 @@ import (
1718
1819// Keys generates a new P256 ECDSA public private key pair for TLS.
1920// It returns a bytes buffer for the PEM encoded private key and certificate.
20- func Keys (validFor time.Duration ) (cert , key * bytes.Buffer , err error ) {
21+ func Keys (validFor time.Duration ) (cert , key * bytes.Buffer , fingerprint [ 32 ] byte , err error ) {
2122 privKey , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
2223 if err != nil {
2324 log .Fatalf ("failed to generate private key: %s" , err )
24- return nil , nil , err
25+ return nil , nil , fingerprint , err
2526 }
2627
2728 notBefore := time .Now ()
@@ -31,7 +32,7 @@ func Keys(validFor time.Duration) (cert, key *bytes.Buffer, err error) {
3132 serialNumber , err := rand .Int (rand .Reader , serialNumberLimit )
3233 if err != nil {
3334 log .Fatalf ("failed to generate serial number: %s" , err )
34- return nil , nil , err
35+ return nil , nil , fingerprint , err
3536 }
3637
3738 template := x509.Certificate {
@@ -50,7 +51,7 @@ func Keys(validFor time.Duration) (cert, key *bytes.Buffer, err error) {
5051 derBytes , err := x509 .CreateCertificate (rand .Reader , & template , & template , & privKey .PublicKey , privKey )
5152 if err != nil {
5253 log .Fatalf ("Failed to create certificate: %s" , err )
53- return nil , nil , err
54+ return nil , nil , fingerprint , err
5455 }
5556
5657 // Encode and write certificate and key to bytes.Buffer
@@ -60,9 +61,9 @@ func Keys(validFor time.Duration) (cert, key *bytes.Buffer, err error) {
6061 key = bytes .NewBuffer ([]byte {})
6162 pem .Encode (key , pemBlockForKey (privKey ))
6263
63- // log.Printf("% X", sha256.Sum256(derBytes) )
64+ fingerprint = sha256 .Sum256 (derBytes )
6465
65- return cert , key , nil
66+ return cert , key , fingerprint , nil //TODO: maybe return a struct instead of 4 multiple return items
6667}
6768
6869func pemBlockForKey (key * ecdsa.PrivateKey ) * pem.Block {
0 commit comments