2021-10-22 00:04:06 -04:00
|
|
|
package tls
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/sha256"
|
2023-02-17 16:01:24 +08:00
|
|
|
"crypto/x509"
|
2026-01-09 08:11:24 +08:00
|
|
|
"encoding/hex"
|
2021-10-22 00:04:06 -04:00
|
|
|
)
|
|
|
|
|
|
2026-01-09 08:11:24 +08:00
|
|
|
// []byte must be ASN.1 DER content
|
|
|
|
|
func GenerateCertHash[T *x509.Certificate | []byte](cert T) []byte {
|
|
|
|
|
var out [32]byte
|
|
|
|
|
switch v := any(cert).(type) {
|
|
|
|
|
case *x509.Certificate:
|
|
|
|
|
out = sha256.Sum256(v.Raw)
|
|
|
|
|
case []byte:
|
|
|
|
|
out = sha256.Sum256(v)
|
2021-10-22 00:04:06 -04:00
|
|
|
}
|
2023-02-17 16:01:24 +08:00
|
|
|
return out[:]
|
|
|
|
|
}
|
2026-02-06 09:57:32 +08:00
|
|
|
|
|
|
|
|
func GenerateCertHashHex[T *x509.Certificate | []byte](cert T) string {
|
|
|
|
|
var out [32]byte
|
|
|
|
|
switch v := any(cert).(type) {
|
|
|
|
|
case *x509.Certificate:
|
|
|
|
|
out = sha256.Sum256(v.Raw)
|
|
|
|
|
case []byte:
|
|
|
|
|
out = sha256.Sum256(v)
|
|
|
|
|
}
|
|
|
|
|
return hex.EncodeToString(out[:])
|
|
|
|
|
}
|