github.com/imannamdari/v2ray-core/v5@v5.0.5/transport/internet/tls/pin.go (about)

     1  package tls
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"encoding/base64"
     6  	"encoding/pem"
     7  )
     8  
     9  func CalculatePEMCertChainSHA256Hash(certContent []byte) string {
    10  	var certChain [][]byte
    11  	for {
    12  		block, remain := pem.Decode(certContent)
    13  		if block == nil {
    14  			break
    15  		}
    16  		certChain = append(certChain, block.Bytes)
    17  		certContent = remain
    18  	}
    19  	certChainHash := GenerateCertChainHash(certChain)
    20  	certChainHashB64 := base64.StdEncoding.EncodeToString(certChainHash)
    21  	return certChainHashB64
    22  }
    23  
    24  func GenerateCertChainHash(rawCerts [][]byte) []byte {
    25  	var hashValue []byte
    26  	for _, certValue := range rawCerts {
    27  		out := sha256.Sum256(certValue)
    28  		if hashValue == nil {
    29  			hashValue = out[:]
    30  		} else {
    31  			newHashValue := sha256.Sum256(append(hashValue, out[:]...))
    32  			hashValue = newHashValue[:]
    33  		}
    34  	}
    35  	return hashValue
    36  }