What is HMAC?

HMAC (Hash-based Message Authentication Code) is a cryptographic technique that combines a secret key with a hash function to verify both the integrity and authenticity of a message. It ensures that data has not been tampered with and comes from a trusted source.

Quick Facts

Full NameHash-based Message Authentication Code
Created1996 (RFC 2104)
SpecificationOfficial Specification

How It Works

HMAC was published in 1996 and is defined in RFC 2104. It works by hashing the message twice with the secret key in a specific way, making it resistant to length extension attacks that affect plain hash functions. HMAC can use any cryptographic hash function like SHA-256 or SHA-512. The resulting code is a fixed-size value that changes completely if either the message or key is modified. HMAC is widely used in API authentication, JWT signatures, and secure communication protocols.

Key Characteristics

  • Combines secret key with hash function
  • Provides both integrity and authenticity
  • Resistant to length extension attacks
  • Can use any hash function (SHA-256, SHA-512)
  • Fixed-size output regardless of input
  • Requires shared secret between parties

Common Use Cases

  1. API request signing
  2. JWT signature verification
  3. Webhook payload verification
  4. Secure cookie signing
  5. Message authentication in protocols

Example

loading...
Loading code...

Frequently Asked Questions

What is the difference between HMAC and a regular hash?

A regular hash only provides data integrity (detecting if data was modified), while HMAC provides both integrity and authenticity (verifying the data came from a trusted source with the secret key). HMAC uses a secret key mixed with the hash function, so only parties with the key can generate or verify the HMAC value.

Why is HMAC more secure than simply hashing a message with a key?

Simply concatenating a key with a message before hashing (e.g., hash(key + message)) is vulnerable to length extension attacks, where attackers can append data to the message without knowing the key. HMAC's double-hashing construction with inner and outer padding specifically prevents this attack and provides proven security guarantees.

Which hash algorithm should I use with HMAC?

HMAC-SHA256 is the most widely recommended choice, offering a good balance of security and performance. HMAC-SHA512 provides extra security margin for highly sensitive applications. Avoid HMAC-MD5 and HMAC-SHA1 for new implementations, though they remain more secure than the underlying hash functions when used in HMAC construction.

How is HMAC used in API authentication?

In API authentication, the client creates an HMAC signature by hashing request details (method, path, timestamp, body) with a shared secret key. The server recreates the same HMAC using its copy of the secret key and compares it to the received signature. If they match, the request is authenticated and hasn't been tampered with.

What happens if the HMAC secret key is compromised?

If the secret key is compromised, attackers can forge valid HMAC signatures for any message, completely bypassing the authentication. You should immediately rotate (change) the compromised key and invalidate all tokens or signatures created with the old key. Use proper key management practices like secure storage and regular rotation to minimize this risk.

Related Tools

Related Terms

Related Articles