回调签名改为与开放 API 一致的字典序拼接风格

- 签名串由 timestamp+换行+bodyhash 改为 body_sha256=<hash>&timestamp=<unix秒>(ASCII 字典序 & 拼接)
- 同步更新对接文档
This commit is contained in:
yml2213
2026-08-03 11:48:10 +08:00
parent 3e8526d5d7
commit ef8b8a7213
2 changed files with 8 additions and 4 deletions
+6 -2
View File
@@ -379,10 +379,14 @@ func (s *CallbackService) Run(ctx context.Context) {
}
}
// BuildCallbackSign 供接收方校验HMAC-SHA256(secret, timestamp + "\n" + sha256(body))。
// BuildCallbackSign 供接收方校验。签名风格与开放 API 一致:
// 参数按 ASCII 字典序 + "&" 拼接,body 以 SHA256 摘要参与签名。
//
// content = body_sha256=<sha256hex(body)>&timestamp=<unix秒>
// X-Sign = hex(HMAC-SHA256(secret, content))
func BuildCallbackSign(secret, timestamp, body string) string {
bodyHash := sha256.Sum256([]byte(body))
content := timestamp + "\n" + hex.EncodeToString(bodyHash[:])
content := "body_sha256=" + hex.EncodeToString(bodyHash[:]) + "&timestamp=" + timestamp
mac := hmac.New(sha256.New, []byte(secret))
_, _ = mac.Write([]byte(content))
return hex.EncodeToString(mac.Sum(nil))