pin.go 797 B

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