回调签名改为与开放 API 一致的字典序拼接风格
- 签名串由 timestamp+换行+bodyhash 改为 body_sha256=<hash>×tamp=<unix秒>(ASCII 字典序 & 拼接) - 同步更新对接文档
This commit is contained in:
@@ -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)>×tamp=<unix秒>
|
||||||
|
// X-Sign = hex(HMAC-SHA256(secret, content))
|
||||||
func BuildCallbackSign(secret, timestamp, body string) string {
|
func BuildCallbackSign(secret, timestamp, body string) string {
|
||||||
bodyHash := sha256.Sum256([]byte(body))
|
bodyHash := sha256.Sum256([]byte(body))
|
||||||
content := timestamp + "\n" + hex.EncodeToString(bodyHash[:])
|
content := "body_sha256=" + hex.EncodeToString(bodyHash[:]) + "×tamp=" + timestamp
|
||||||
mac := hmac.New(sha256.New, []byte(secret))
|
mac := hmac.New(sha256.New, []byte(secret))
|
||||||
_, _ = mac.Write([]byte(content))
|
_, _ = mac.Write([]byte(content))
|
||||||
return hex.EncodeToString(mac.Sum(nil))
|
return hex.EncodeToString(mac.Sum(nil))
|
||||||
|
|||||||
@@ -615,7 +615,7 @@ function CallbackSignSection() {
|
|||||||
<Text code>sha256hex(body)</Text> —— 原始请求体字节的 SHA256 十六进制(小写)
|
<Text code>sha256hex(body)</Text> —— 原始请求体字节的 SHA256 十六进制(小写)
|
||||||
</Descriptions.Item>
|
</Descriptions.Item>
|
||||||
<Descriptions.Item label="签名串">
|
<Descriptions.Item label="签名串">
|
||||||
<Text code>timestamp + "\n" + sha256hex(body)</Text>,换行符拼接
|
<Text code>body_sha256=<hash>&timestamp=<unix秒></Text>,按 ASCII 字典序 <Text code>&</Text> 拼接(与开放 API 鉴权同一风格)
|
||||||
</Descriptions.Item>
|
</Descriptions.Item>
|
||||||
<Descriptions.Item label="X-Sign">
|
<Descriptions.Item label="X-Sign">
|
||||||
<Text code>hex( HMAC-SHA256( secret, 签名串 ) )</Text>,小写十六进制
|
<Text code>hex( HMAC-SHA256( secret, 签名串 ) )</Text>,小写十六进制
|
||||||
@@ -624,7 +624,7 @@ function CallbackSignSection() {
|
|||||||
</Card>
|
</Card>
|
||||||
<Card className="api-docs__card" size="small" title="校验伪代码">
|
<Card className="api-docs__card" size="small" title="校验伪代码">
|
||||||
<pre className="api-docs__pre">{`bodyHash = sha256Hex(rawBody)
|
<pre className="api-docs__pre">{`bodyHash = sha256Hex(rawBody)
|
||||||
content = timestamp + "\\n" + bodyHash
|
content = "body_sha256=" + bodyHash + "×tamp=" + timestamp
|
||||||
expected = hmacSHA256Hex(secret, content)
|
expected = hmacSHA256Hex(secret, content)
|
||||||
ok = (X-Sign == expected)`}</pre>
|
ok = (X-Sign == expected)`}</pre>
|
||||||
</Card>
|
</Card>
|
||||||
|
|||||||
Reference in New Issue
Block a user