Un Keyed-Hash Message Authentication Code (HMAC) dimostra sia l'integrità dei dati (il payload non è stato alterato durante il transito) sia l'autenticità (il mittente possiede la chiave segreta condivisa). Riprodurre la firma di un provider richiede la corrispondenza esatta di tre parametri: la primitiva di hash sottostante, la decodifica in byte grezzi della chiave segreta e la sequenza esatta di byte del payload firmato.
Esempio 1: Caso di Test 2 IETF RFC 2104 / RFC 2202 / RFC 4231
Sia che tu inserisca la chiave segreta di 4 byte come testo UTF-8 Jefe, esadecimale grezzo 4a656665 o Base64 SmVmZQ==, essa viene decodificata nell'identico buffer di chiave [0x4a, 0x65, 0x66, 0x65]. Firmando il messaggio UTF-8 di 28 byte what do ya want for nothing? si ottiene:
{
"keyUtf8": "Jefe",
"keyHex": "4a656665",
"keyBase64": "SmVmZQ==",
"message": "what do ya want for nothing?",
"signatures": {
"hmac_md5_hex": "750c783e6ab0b503eaa86e310a5db738",
"hmac_sha1_hex": "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
"hmac_sha256_hex": "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843",
"hmac_sha256_base64": "W9zBRr9gdU5qBCQmCJV1x1oAPwidJzmDnexYuWTsOEM=",
"hmac_sha256_base64url": "W9zBRr9gdU5qBCQmCJV1x1oAPwidJzmDnexYuWTsOEM",
"hmac_sha384_hex": "af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649",
"hmac_sha512_hex": "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737"
}
}
Esempio 2: Base64 vs Base64URL e Payload Canonici dei Webhook
Il formato Base64 standard (RFC 4648 Sezione 4) emette +, / e il padding finale =, mentre il formato URL-safe Base64URL (RFC 4648 Sezione 5, utilizzato nelle firme JWS/JWT e negli URL firmati) sostituisce + con -, / con _ e rimuove il padding =. Firmare The quick brown fox jumps over the lazy dog con la chiave UTF-8 key sotto HMAC-SHA256 illustra direttamente la differenza di codifica:
Key (UTF-8): key
Message (UTF-8): The quick brown fox jumps over the lazy dog
HMAC-SHA256 Hex: f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8
HMAC-SHA256 Base64: 97yD9DBThCSxMpjmqm+xQ+9NWaFJRhdZl0edvC0aPNg=
HMAC-SHA256 Base64URL: 97yD9DBThCSxMpjmqm-xQ-9NWaFJRhdZl0edvC0aPNg
Quando verifichi gli header dei webhook reali, costruisci l'esatto payload pre-firma definito da ciascun provider prima di rimuovere i prefissi degli header:
GitHub (X-Hub-Signature-256: sha256=<hex>):
Signed input = <raw_http_body>
Stripe (Stripe-Signature: t=1730419200,v1=<hex>):
Signed input = 1730419200.<raw_http_body>
Slack (X-Slack-Signature: v0=<hex>):
Signed input = v0:1730419200:<raw_http_body>
Shopify (X-Shopify-Hmac-Sha256: <base64>):
Signed input = <raw_http_body>