What is hmac

Last updated: April 1, 2026

Quick Answer: HMAC (Hash-based Message Authentication Code) is a cryptographic technique that combines a secret key with a hash function to verify both the authenticity and integrity of a message, ensuring it hasn't been tampered with or forged.

Key Facts

Introduction to HMAC

HMAC stands for Hash-based Message Authentication Code, a cryptographic technique used to verify both the authenticity and integrity of a message. It combines a shared secret key with a message and applies a cryptographic hash function to produce a unique code that proves the message hasn't been altered and comes from an authenticated source.

How HMAC Works

HMAC operates through a straightforward but powerful process involving several cryptographic operations. The sender uses a secret key and the message to calculate an HMAC value. This value is then transmitted along with the original message to the recipient. The recipient uses their own copy of the secret key to independently calculate an HMAC from the received message. If both HMAC values match, the message is authenticated and unaltered. If they differ, the message has been tampered with or the sender is not who they claim to be.

Technical Construction

HMAC combines three main components to ensure security:

The HMAC algorithm applies the hash function twice: first combining the key with the message, then hashing the result again with a modified key. This double-hashing approach significantly strengthens security against various cryptographic attacks.

Common Applications

HMAC is widely deployed across internet security and data protection:

HMAC vs. Digital Signatures

While both HMAC and digital signatures provide authentication, they work differently. HMAC uses a symmetric secret key that both parties must share, making it efficient for internal systems or trusted partners. Digital signatures use asymmetric cryptography with public and private keys, allowing verification by anyone with the public key, which is essential for public authentication scenarios.

Security Considerations

The security of HMAC depends entirely on the secrecy of the shared key. If the key is compromised, any party can forge valid HMACs. Therefore, secret keys must be generated securely, stored safely, and rotated periodically. Organizations should never hardcode secret keys in source code or share them over insecure channels.

Related Questions

What is the difference between HMAC and a digital signature?

HMAC uses a shared secret key for symmetric authentication, while digital signatures use asymmetric cryptography with public and private keys. HMAC is faster and suitable for private authentication between trusted parties, whereas digital signatures enable public verification of authenticity.

Can HMAC be used for encryption?

No, HMAC is designed exclusively for authentication and integrity verification, not encryption. It produces a hash code that proves a message is authentic but does not conceal the message contents. For confidentiality, encryption algorithms like AES must be used separately.

What hash functions are used with HMAC?

Common hash functions paired with HMAC include SHA-256, SHA-512, and SHA-1 (though SHA-1 is deprecated). SHA-256 and SHA-512 are recommended for new implementations due to their strong security properties and resistance to collision attacks.

Sources

  1. Wikipedia - HMAC CC-BY-SA-4.0
  2. NIST FIPS 198-1 - The Keyed-Hash Message Authentication Code Public Domain