-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls_fingerprints.go
55 lines (46 loc) · 1.06 KB
/
tls_fingerprints.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
package main
import (
"bytes"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"fmt"
)
type fingerprintFunction func([]byte) []byte
var fingerprintFunctions = map[string]fingerprintFunction{
"MD5": calcMD5Fingerprint,
"SHA1": calcSHA1Fingerprint,
"SHA256": calcSHA256Fingerprint,
}
// formatFingerprint calculates and then formats the different hash digests
// of the DER-encoded PublicKey from an X.509 certificate.
func formatFingerprint(cert []byte) map[string]interface{} {
m := make(map[string]interface{})
var buf bytes.Buffer
for hash, fingerprint := range fingerprintFunctions {
buf.Reset()
for i, b := range fingerprint(cert) {
if i > 0 {
fmt.Fprintf(&buf, ":")
}
fmt.Fprintf(&buf, "%02x", b)
}
m[hash] = buf.String()
}
return m
}
func calcSHA1Fingerprint(rpki []byte) []byte {
h := sha1.New()
h.Write(rpki)
return h.Sum(nil)
}
func calcMD5Fingerprint(rpki []byte) []byte {
h := md5.New()
h.Write(rpki)
return h.Sum(nil)
}
func calcSHA256Fingerprint(rpki []byte) []byte {
h := sha256.New()
h.Write(rpki)
return h.Sum(nil)
}