This commit is contained in:
2025-07-07 20:11:59 +08:00
parent ab0fdbc447
commit 06e3aa2eb3
2009 changed files with 193082 additions and 0 deletions

21
api/saft_hat/verify.go Normal file
View File

@ -0,0 +1,21 @@
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) // 比较计算得到的摘要和传入的签名
}