-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
59 lines (51 loc) · 1.32 KB
/
main.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
package encryption
import (
"crypto/ecdsa"
"crypto/md5"
"crypto/rsa"
"crypto/sha1"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"github.com/cjlapao/common-go/log"
)
var logger = log.Get()
func GetKeyBytes(key interface{}) []byte {
var encodedBytes []byte
switch k := key.(type) {
case *rsa.PrivateKey:
encodedBytes = x509.MarshalPKCS1PrivateKey(k)
return encodedBytes
case *rsa.PublicKey:
encodedBytes = x509.MarshalPKCS1PublicKey(k)
return encodedBytes
case *ecdsa.PrivateKey:
encodedBytes, _ = x509.MarshalECPrivateKey(k)
return encodedBytes
case *ecdsa.PublicKey:
encodedBytes, _ = x509.MarshalPKIXPublicKey(k)
return encodedBytes
default:
return make([]byte, 0)
}
}
func GetSHA2KeyFingerprint(key interface{}) [32]byte {
encodedBytes := GetKeyBytes(key)
fingerprint := sha256.Sum256(encodedBytes)
return fingerprint
}
func GetSHA1KeyFingerprint(key interface{}) [20]byte {
encodedBytes := GetKeyBytes(key)
fingerprint := sha1.Sum(encodedBytes)
return fingerprint
}
func GetMD5KeyFingerprint(key interface{}) [16]byte {
encodedBytes := GetKeyBytes(key)
fingerprint := md5.Sum(encodedBytes)
return fingerprint
}
func GetBase64KeyFingerprint(key interface{}) string {
fingerprintBytes := GetSHA2KeyFingerprint(key)
encoded := base64.URLEncoding.EncodeToString(fingerprintBytes[:])
return encoded
}