2024-08-30 18:02:15 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-10-23 15:53:21 +08:00
|
|
|
// getCert 从证书数据中提取证书
|
2024-08-30 18:02:15 +08:00
|
|
|
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
|
|
|
|
}
|