1. The Problem with Naive Hash Keying
Simply concatenating a secret key and message as `hash(key + message)` is vulnerable to length-extension attacks in Merkle-Damgård algorithms (like SHA-256 and MD5). An attacker can append additional data and compute a valid signature without knowing the secret key.
2. The HMAC Nested Construction (RFC 2104)
HMAC resolves length-extension attacks by using a nested two-pass hashing structure with inner and outer padding constants (ipad and opad):
TEXTSyntax Example
HMAC(K, m) = H((K' ⊕ opad) ∥ H((K' ⊕ ipad) ∥ m))
Where:
• H is the cryptographic hash function (e.g. SHA-256)
• K' is the normalized key derived from secret key K
• ipad = 0x36 repeated to block length (inner pad)
• opad = 0x5c repeated to block length (outer pad)3. Real-World Webhook and API Security
HMAC-SHA256 is the industry standard for API request signing and webhook security across Stripe, GitHub, Shopify, and AWS Signature Version 4. It guarantees both data integrity and the authentic identity of the sender.