回调签名改为与开放 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))
+2 -2
View File
@@ -615,7 +615,7 @@ function CallbackSignSection() {
<Text code>sha256hex(body)</Text> SHA256
</Descriptions.Item>
<Descriptions.Item label="签名串">
<Text code>timestamp + "\n" + sha256hex(body)</Text>
<Text code>body_sha256=&lt;hash&gt;&amp;timestamp=&lt;unix秒&gt;</Text> ASCII <Text code>&amp;</Text> API
</Descriptions.Item>
<Descriptions.Item label="X-Sign">
<Text code>hex( HMAC-SHA256( secret, ) )</Text>
@@ -624,7 +624,7 @@ function CallbackSignSection() {
</Card>
<Card className="api-docs__card" size="small" title="校验伪代码">
<pre className="api-docs__pre">{`bodyHash = sha256Hex(rawBody)
content = timestamp + "\\n" + bodyHash
content = "body_sha256=" + bodyHash + "&timestamp=" + timestamp
expected = hmacSHA256Hex(secret, content)
ok = (X-Sign == expected)`}</pre>
</Card>