HMAC
Hash-Based Message Authentication Code · Also known as: HMAC, keyed hash function
HMAC (Hash-Based Message Authentication Code) is a cryptographic algorithm for authenticating messages using a secret key and a hash function. Standardized in RFC 2104 (1997), HMAC can be combined with any cryptographic hash function (SHA-256, SHA-3, etc.) to create a message authentication code (MAC). HMAC provides both data integrity and authentication, detecting both accidental corruption and deliberate tampering, and is widely used in web security (TLS/SSL), API authentication, and network protocols.
Read the full method
Sign in with a free account to read this section.
Method map
The neighbourhood of related methods — select a node to explore.
When to use it
HMAC is essential for message authentication in protocols where both parties share a secret key. Use HMAC when you need to verify that a message has not been modified and that it came from the authenticated source. Common applications include API authentication (signing requests with HMAC-SHA256), session management, and in TLS/SSL for additional authentication layers. HMAC is suitable for both high-speed and security-critical applications.
Strengths & limitations
- Provides both message integrity and authentication in a single operation
- Simple and efficient to compute, with minimal computational overhead compared to digital signatures
- Compatible with any underlying hash function, offering flexibility in algorithm choice
- Theoretically proven secure if the underlying hash function behaves like a random oracle
- Requires secure shared key management; both sender and receiver must possess the same secret key
- Does not provide non-repudiation; both parties can deny creating or authorizing a message
- Vulnerable to key recovery if the underlying hash function is broken or misused
- Timing attacks on HMAC comparison can leak information about the correct code if verification is not constant-time
Frequently asked
Why is HMAC better than just concatenating the key and message before hashing?
Simple concatenation (key || message) is vulnerable to length extension attacks. An attacker can compute HMAC(key || message || x) without knowing the key if they know the original HMAC. HMAC's nested design with separate padding prevents this attack.
How do I compare HMACs securely?
Use constant-time comparison functions (like secrets.compare_digest in Python) instead of standard string equality. This prevents timing attacks that allow an attacker to forge valid HMACs by observing how long comparison takes.
Can I use HMAC for key derivation?
HMAC alone is not a strong key derivation function. Use HKDF (HMAC-based Key Derivation Function) or PBKDF2 instead, which apply HMAC multiple times with additional randomness to generate cryptographic keys from passwords or shared secrets.
Is HMAC quantum-resistant?
HMAC's security relies on the underlying hash function. If the hash function is quantum-resistant, then HMAC is also quantum-resistant. SHA-3 and other post-quantum hash functions can be used with HMAC for future-proofing.
What hash function should I use with HMAC?
Use SHA-256 or SHA-3 for new systems. SHA-1 is deprecated for most uses. Avoid MD5. The choice depends on your security requirements and compatibility needs.
Sources
- Krawczyk, H., Bellare, M., & Crechanko, R. (1997). HMAC: Keyed-Hashing for Message Authentication. RFC 2104. link ↗
- Bellare, M., Canetti, R., & Krawczyk, H. (1996). Keying hash functions for message authentication. In Advances in Cryptology - CRYPTO 1996, LNCS 1109, pp. 1-15. DOI: 10.1007/3-540-68697-5_1 ↗
How to cite this page
ScholarGate. (2026, June 3). Hash-Based Message Authentication Code. ScholarGate. https://scholargate.app/en/cryptography/hmac
Which method?
Set this method beside its closest kin and read them side by side — the library lays the books on the table; the choice is yours.
- AES (Rijndael)Cryptography↔ compare
- Differential CryptanalysisCryptography↔ compare
- RSA CryptosystemCryptography↔ compare