5
5
"crypto/ecdsa"
6
6
"crypto/elliptic"
7
7
"crypto/rand"
8
+ "crypto/sha256"
8
9
"crypto/x509"
9
10
"crypto/x509/pkix"
10
11
"encoding/pem"
@@ -17,11 +18,11 @@ import (
17
18
18
19
// Keys generates a new P256 ECDSA public private key pair for TLS.
19
20
// 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 ) {
21
22
privKey , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
22
23
if err != nil {
23
24
log .Fatalf ("failed to generate private key: %s" , err )
24
- return nil , nil , err
25
+ return nil , nil , fingerprint , err
25
26
}
26
27
27
28
notBefore := time .Now ()
@@ -31,7 +32,7 @@ func Keys(validFor time.Duration) (cert, key *bytes.Buffer, err error) {
31
32
serialNumber , err := rand .Int (rand .Reader , serialNumberLimit )
32
33
if err != nil {
33
34
log .Fatalf ("failed to generate serial number: %s" , err )
34
- return nil , nil , err
35
+ return nil , nil , fingerprint , err
35
36
}
36
37
37
38
template := x509.Certificate {
@@ -50,7 +51,7 @@ func Keys(validFor time.Duration) (cert, key *bytes.Buffer, err error) {
50
51
derBytes , err := x509 .CreateCertificate (rand .Reader , & template , & template , & privKey .PublicKey , privKey )
51
52
if err != nil {
52
53
log .Fatalf ("Failed to create certificate: %s" , err )
53
- return nil , nil , err
54
+ return nil , nil , fingerprint , err
54
55
}
55
56
56
57
// Encode and write certificate and key to bytes.Buffer
@@ -60,9 +61,9 @@ func Keys(validFor time.Duration) (cert, key *bytes.Buffer, err error) {
60
61
key = bytes .NewBuffer ([]byte {})
61
62
pem .Encode (key , pemBlockForKey (privKey ))
62
63
63
- // log.Printf("% X", sha256.Sum256(derBytes) )
64
+ fingerprint = sha256 .Sum256 (derBytes )
64
65
65
- return cert , key , nil
66
+ return cert , key , fingerprint , nil //TODO: maybe return a struct instead of 4 multiple return items
66
67
}
67
68
68
69
func pemBlockForKey (key * ecdsa.PrivateKey ) * pem.Block {
0 commit comments