HMAC Generator ยท 6 min read
What Is HMAC and Why Is It Used for API Authentication?
HMAC combines a cryptographic hash with a secret key to produce an authentication code. Learn how it works, why it is better than a plain hash for API signing, and where you have already used it.
The Problem HMAC Solves
Cryptographic hash functions like SHA-256 are excellent for verifying data integrity โ if a file's SHA-256 hash matches a known value, the file has not been modified. But a plain hash alone cannot prove who created it. Anyone can compute SHA-256 of any data. To authenticate that a message came from a specific party, you need to involve a secret that only that party possesses.
HMAC โ Hash-based Message Authentication Code โ solves this by incorporating a secret key into the hash computation. The result is a code that proves both that the message has not been tampered with (integrity) and that it was created by someone who knows the secret key (authentication).
The HMAC Construction
HMAC is defined in RFC 2104 by Bellare, Canetti, and Krawczyk. Its construction is deliberately specific to avoid security flaws in naive approaches:
HMAC(K, m) = H((K' XOR opad) || H((K' XOR ipad) || m))
Where H is the underlying hash function (SHA-256, SHA-512, etc.), K' is the key padded to the block size, ipad is the inner padding constant (0x36 repeated), and opad is the outer padding constant (0x5C repeated). The double-hash structure is intentional โ it prevents a specific class of attack.
Why Not Just Prepend the Key to the Message?
A naive approach would be to compute SHA-256(key || message). This is insecure because of the length extension attack: SHA-256's internal state at the end of processing one message can be used as a starting point to compute the hash of that message extended with additional data โ without knowing the key. An attacker could forge a valid "signed" message by appending data.
HMAC's double-hash construction specifically prevents this. The inner hash processes the key and message; the outer hash processes the key again with the inner hash result. Length extension attacks cannot penetrate this two-layer structure.
HMAC Properties
- Authentication: Only someone who knows the secret key can produce a valid HMAC โ verifying the origin of the message
- Integrity: Any change to the message changes the HMAC value, making tampering detectable
- Replay protection: When combined with a timestamp or nonce in the message, HMAC prevents replay attacks (reusing a captured valid request)
HMAC provides authentication and integrity but not confidentiality โ the message content is not encrypted. For confidential authenticated communication, combine HMAC with encryption (or use an authenticated encryption mode like AES-GCM).
Where HMAC Is Used
- AWS Signature Version 4: Every AWS API request is signed using HMAC-SHA256 with derived signing keys based on the date, region, and service. This proves the request came from someone with valid AWS credentials and prevents tampering.
- Stripe webhook verification: Stripe signs webhook payloads with HMAC-SHA256. Your application verifies the signature before processing the event to confirm it came from Stripe.
- TOTP (Time-based One-Time Passwords): The 6-digit codes from authenticator apps are generated using HMAC-SHA1 with the shared secret as the key and the current time window as the message.
- Cookie signing: Web frameworks sign session cookies with HMAC to prevent users from forging or modifying cookie values without the server's secret.
- JWT (HS256): The HMAC-SHA256 signing algorithm in JSON Web Tokens uses an HMAC to sign the header and payload.
Constant-Time Comparison
When verifying an HMAC, you must compare the expected value to the received value using a constant-time comparison function. Standard string comparison in most languages short-circuits on the first mismatched character โ revealing timing information an attacker can exploit to forge valid HMACs character by character. Cryptographic libraries provide timing-safe comparison functions (hmac.compare_digest() in Python, crypto.timingSafeEqual() in Node.js) that always take the same time regardless of where the strings differ.
HMAC vs Digital Signatures
HMAC uses a symmetric key: the same secret is used to create and verify the code. Both parties must share the secret โ meaning anyone who can verify an HMAC could also have created it. Digital signatures (RSA, ECDSA) use an asymmetric key pair: a private key signs, a public key verifies. A digital signature proves the message came from the holder of the private key โ even to parties who can only verify, not sign. HMAC is simpler and faster; digital signatures provide non-repudiation.
References
- Krawczyk, H., Bellare, M., & Canetti, R. (1997). RFC 2104: HMAC: Keyed-Hashing for Message Authentication. Internet Engineering Task Force.
- NIST. (2008). FIPS PUB 198-1: The Keyed-Hash Message Authentication Code (HMAC). National Institute of Standards and Technology.
- Bellare, M., Canetti, R., & Krawczyk, H. (1996). Keying Hash Functions for Message Authentication. Advances in Cryptology โ CRYPTO '96.
- Amazon Web Services. (2024). Signature Version 4 Signing Process. AWS General Reference Documentation.