Machine learningCryptographyMessage authentication codeAlgorithm

HMAC

Also known as: HMAC, keyed hash function

OriginatorHugo KrawczykYear1997Sources2Related methods8

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.

Key highlights

  • 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

Intuition

This section is available to Pro members. Upgrade to Pro

How it works

This section is available to Pro members. Upgrade to Pro

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

Strengths
  • 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
Limitations
  • 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

Common pitfalls

This section is available to Pro members. Upgrade to Pro

Applications

This section is available to Pro members. Upgrade to Pro

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

  1. 1.
    Krawczyk, H., Bellare, M., & Crechanko, R. (1997). HMAC: Keyed-Hashing for Message Authentication. RFC 2104.
  2. 2.
    Bellare, M., Canetti, R., & Krawczyk, H. (1996). Keying hash functions for message authentication. In Advances in Cryptology - CRYPTO 1996, LNCS 1109, pp. 1-15.

You have read it. What now?

Cite this page

ScholarGate. (2026, June 3). HMAC. ScholarGate. https://scholargate.app/cryptography/hmac

HMAC — Hash-Based Message Authentication Code | ScholarGate