X-Ake-Signature 请求头,使用您在后台配置的 Webhook Signing Secret(whsec_...)计算签名:
X-Ake-Signature: t=1764215400,v1=9a7d2b44e0f3e6d1...
验签步骤
- 从
X-Ake-Signature中解析时间戳t(Unix 秒数)与签名摘要v1(小写十六进制)。 - 校验
t与当前服务器 UTC 时间的差值在允许容差内(建议 300 秒),防止重放攻击。 - 按
${t}.${rawRequestBody}拼接待签名原始字符串(必须使用 JSON 解析前的原始 UTF-8 字节流)。 - 使用密钥计算
HMAC-SHA256,并通过**恒定时间比较函数(constant-time compare)**与v1比对。
多语言验签示例
import crypto from "node:crypto";
export function verifyAkeWebhook(rawBody, signatureHeader, secret) {
const parts = Object.fromEntries(
signatureHeader.split(",").map((kv) => kv.split("="))
);
const timestamp = Number(parts.t);
const expectedSig = parts.v1;
if (!timestamp || !expectedSig) return false;
if (Math.abs(Math.floor(Date.now() / 1000) - timestamp) > 300) {
return false;
}
const signedPayload = `${timestamp}.${rawBody}`;
const computed = crypto
.createHmac("sha256", secret)
.update(signedPayload, "utf8")
.digest("hex");
const a = Buffer.from(computed, "hex");
const b = Buffer.from(expectedSig, "hex");
return a.length === b.length && crypto.timingSafeEqual(a, b);
}
import hashlib
import hmac
import time
def verify_ake_webhook(raw_body: bytes, signature_header: str, secret: str) -> bool:
parts = dict(item.split("=", 1) for item in signature_header.split(",") if "=" in item)
timestamp = int(parts.get("t", "0"))
expected_sig = parts.get("v1", "")
if abs(int(time.time()) - timestamp) > 300:
return False
signed_payload = f"{timestamp}.".encode("utf-8") + raw_body
computed = hmac.new(
secret.encode("utf-8"), signed_payload, hashlib.sha256
).hexdigest()
return hmac.compare_digest(computed, expected_sig)
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.time.Instant;
import java.util.HexFormat;
public class AkeWebhookVerifier {
public static boolean verify(String rawBody, String header, String secret) throws Exception {
String[] parts = header.split(",");
long timestamp = 0;
String sigHex = "";
for (String part : parts) {
if (part.startsWith("t=")) timestamp = Long.parseLong(part.substring(2));
if (part.startsWith("v1=")) sigHex = part.substring(3);
}
if (Math.abs(Instant.now().getEpochSecond() - timestamp) > 300) return false;
String payload = timestamp + "." + rawBody;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
String computed = HexFormat.of().formatHex(mac.doFinal(payload.getBytes(StandardCharsets.UTF_8)));
return MessageDigest.isEqual(
computed.getBytes(StandardCharsets.UTF_8),
sigHex.getBytes(StandardCharsets.UTF_8)
);
}
}
package webhook
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"math"
"strconv"
"strings"
"time"
)
func VerifySignature(rawBody []byte, header, secret string) bool {
var ts int64
var v1 string
for _, p := range strings.Split(header, ",") {
if strings.HasPrefix(p, "t=") {
ts, _ = strconv.ParseInt(strings.TrimPrefix(p, "t="), 10, 64)
} else if strings.HasPrefix(p, "v1=") {
v1 = strings.TrimPrefix(p, "v1=")
}
}
if ts == 0 || v1 == "" {
return false
}
if math.Abs(float64(time.Now().Unix()-ts)) > 300 {
return false
}
mac := hmac.New(sha256.New, []byte(secret))
mac.Write([]byte(fmt.Sprintf("%d.%s", ts, string(rawBody))))
expected := hex.EncodeToString(mac.Sum(nil))
return hmac.Equal([]byte(expected), []byte(v1))
}