mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-05-08 14:13:22 +00:00
Usage: https://github.com/XTLS/Xray-core/pull/5507 --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: RPRX <63339210+RPRX@users.noreply.github.com>
40 lines
794 B
Go
40 lines
794 B
Go
package tls
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"crypto/x509"
|
|
"encoding/hex"
|
|
"encoding/pem"
|
|
)
|
|
|
|
func CalculatePEMLeafCertSHA256Hash(certContent []byte) (string, error) {
|
|
var leafCert *x509.Certificate
|
|
for {
|
|
var err error
|
|
block, remain := pem.Decode(certContent)
|
|
if block == nil {
|
|
break
|
|
}
|
|
leafCert, err = x509.ParseCertificate(block.Bytes)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
certContent = remain
|
|
}
|
|
certHash := GenerateCertHash(leafCert)
|
|
certHashHex := hex.EncodeToString(certHash)
|
|
return certHashHex, nil
|
|
}
|
|
|
|
// []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)
|
|
}
|
|
return out[:]
|
|
}
|