22 lines
697 B
Go
22 lines
697 B
Go
package saft_hat
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha1"
|
|
"encoding/hex"
|
|
"log"
|
|
)
|
|
|
|
// 函数用于验证请求的签名
|
|
func VerifySignature(message, messageSignature, secret string) bool {
|
|
mac := hmac.New(sha1.New, []byte(secret)) // 初始化HMAC-SHA1
|
|
mac.Write([]byte(message)) // 写入消息体以计算摘要
|
|
expectedMAC := mac.Sum(nil) // 计算消息的摘要
|
|
signature, err := hex.DecodeString(messageSignature) // 将签名从十六进制字符串解码
|
|
if err != nil {
|
|
log.Printf("解码失败: %v", err)
|
|
return false
|
|
}
|
|
return hmac.Equal(signature, expectedMAC) // 比较计算得到的摘要和传入的签名
|
|
}
|