-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx509.go
60 lines (52 loc) · 1.29 KB
/
x509.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
package rsa
import (
"crypto/md5"
"crypto/sha1"
stdx509 "crypto/x509"
"encoding/binary"
"hash"
)
type x509 struct{}
var X509 x509
// Calculates issuer hash value from cert
func (x509) IssuerHash(cert *stdx509.Certificate) (uint32, error) {
if cert == nil {
return 0, NilInputError(0)
}
return X509.hash(cert.RawIssuer, sha1.New())
}
// Calculates old-style (MD5) issuer hash value from cert
func (x509) IssuerHashOld(cert *stdx509.Certificate) (uint32, error) {
if cert == nil {
return 0, NilInputError(0)
}
return X509.hash(cert.RawIssuer, md5.New())
}
// Calculates subject hash value from cert
func (x509) SubjectHash(cert *stdx509.Certificate) (uint32, error) {
if cert == nil {
return 0, NilInputError(0)
}
return X509.hash(cert.RawSubject, sha1.New())
}
// Calculates old-style (MD5) subject hash value from cert
func (x509) SubjectHashOld(cert *stdx509.Certificate) (uint32, error) {
if cert == nil {
return 0, NilInputError(0)
}
return X509.hash(cert.RawSubject, md5.New())
}
func (x509) hash(b []byte, h hash.Hash) (uint32, error) {
if b == nil {
return 0, EmptyInputError(0)
}
if h == nil {
return 0, NilInputError(0)
}
if _, err := h.Write(b); err != nil {
return 0, err
}
digest := h.Sum(nil)
hash := binary.LittleEndian.Uint32(digest[:4])
return hash, nil
}