This commit is contained in:
2026-07-25 14:18:14 +08:00
parent cc2513652e
commit fa55098520
7 changed files with 216 additions and 58 deletions

View File

@@ -37,15 +37,20 @@ func isHexDigest(s string, length int) bool {
return true
}
// SignHeaders 按 OpenAPI 规范生成签名请求头
// SignHeaders 按 OpenAPI 规范生成签名请求头
// nonce 传入本地请求流水 ID与日志 request_id 一致),便于上下游按同一 ID 排查;为空时回退随机生成。
// canonical:
//
// clientId={id}\ntimestamp={ms}\nnonce={hex}\nmethod={METHOD}\npath={path}\nbodySha256={sha256}
func SignHeaders(clientID, clientSecret, method, path string, body []byte) (map[string]string, error) {
// clientId={id}\ntimestamp={ms}\nnonce={nonce}\nmethod={METHOD}\npath={path}\nbodySha256={sha256}
func SignHeaders(clientID, clientSecret, method, path string, body []byte, nonce string) (map[string]string, error) {
timestamp := strconv.FormatInt(time.Now().UnixMilli(), 10)
nonce, err := randomNonce(16)
if err != nil {
return nil, fmt.Errorf("生成 nonce 失败: %w", err)
nonce = strings.TrimSpace(nonce)
if nonce == "" {
var err error
nonce, err = randomNonce(16)
if err != nil {
return nil, fmt.Errorf("生成 nonce 失败: %w", err)
}
}
sum := sha256.Sum256(body)