40 lines
785 B
Go
40 lines
785 B
Go
package utils
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"crypto/x509"
|
|
"encoding/hex"
|
|
"encoding/pem"
|
|
"fmt"
|
|
"log"
|
|
)
|
|
|
|
// md5Hash 计算 MD5 哈希值
|
|
func md5Hash(s string) string {
|
|
h := md5.New()
|
|
h.Write([]byte(s))
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
}
|
|
|
|
// hex2dec 将字节数组转换为十六进制字符串
|
|
func hex2dec(hex string) string {
|
|
var dec int
|
|
fmt.Sscanf(hex, "%x", &dec)
|
|
return fmt.Sprintf("%d", dec)
|
|
}
|
|
|
|
// getCert 从证书数据中提取证书
|
|
func getCert(certData []byte) (*x509.Certificate, error) {
|
|
block, _ := pem.Decode(certData)
|
|
if block == nil {
|
|
log.Fatal("Failed to parse PEM block containing the cert")
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse certificate: %v", err)
|
|
}
|
|
|
|
return cert, nil
|
|
}
|