-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyid.go
68 lines (63 loc) · 1.61 KB
/
keyid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"bytes"
"strings"
"encoding/base32"
"fmt"
"os"
"io/ioutil"
"crypto"
"crypto/x509"
"encoding/pem"
)
// copied from docker/libtrust
func keyIDEncode(b []byte) string {
s := strings.TrimRight(base32.StdEncoding.EncodeToString(b), "=")
var buf bytes.Buffer
var i int
for i = 0; i < len(s)/4-1; i++ {
start := i * 4
end := start + 4
buf.WriteString(s[start:end] + ":")
}
buf.WriteString(s[i*4:])
return buf.String()
}
// copied from docker/libtrust
func keyIDFromCryptoKey(pubKey crypto.PublicKey) string {
// Generate and return a 'libtrust' fingerprint of the public key.
// For an RSA key this should be:
// SHA256(DER encoded ASN1)
// Then truncated to 240 bits and encoded into 12 base32 groups like so:
// ABCD:EFGH:IJKL:MNOP:QRST:UVWX:YZ23:4567:ABCD:EFGH:IJKL:MNOP
derBytes, err := x509.MarshalPKIXPublicKey(pubKey)
if err != nil {
return ""
}
hasher := crypto.SHA256.New()
hasher.Write(derBytes)
h := hasher.Sum(nil)[:30]
return keyIDEncode(h)
}
func main() {
if len(os.Args) < 2 {
panic("no filename given")
}
filename := os.Args[1]
fp, err := os.Open(filename)
if err != nil {
msg := fmt.Sprint("Can not open ", filename)
panic(msg)
}
rawData, err := ioutil.ReadAll(fp)
if err != nil {
panic("Can not read data")
}
pemBlock, rawData := pem.Decode(rawData)
cert, err := x509.ParseCertificate(pemBlock.Bytes)
if err != nil {
panic("Can not parse certificate")
}
key := crypto.PublicKey(cert.PublicKey)
fmt.Println(keyIDFromCryptoKey(key))
}