Course contentsShow
← Security
Lesson 217 of 3,0525. Cryptographic Hashing and MACsFree lesson

HMAC Construction and Security Properties

Learn how HMAC combines a hash function with a secret key to provide message authentication and integrity.

HMAC Construction and Security Properties

What you'll learn: How HMAC combines a hash function with a secret key to guarantee that a message hasn't been tampered with and comes from someone who knows the key.

What Is HMAC?

HMAC (Hash-based Message Authentication Code) is a cryptographic construction that wraps a hash function (like SHA-256) around a secret key to create a message authentication tag. Unlike plain hashing, where anyone can compute hash(message), HMAC requires knowledge of a secret key. Only someone with that key can generate or verify the correct HMAC tag.

Think of it like a wax seal on an envelope. The pattern (hash function) is visible to everyone, but only someone with the correct signet ring (secret key) can create an authentic seal.

How HMAC Works

HMAC applies the hash function twice with the key mixed in using XOR operations:

HMAC(key, message) = H((key ⊕ opad) || H((key ⊕ ipad) || message))

Where ipad and opad are fixed padding constants, and || means concatenation. This "double hashing" structure is what makes HMAC secure.

Why This Matters

HMAC provides two critical security properties:

  1. Authenticity: Only someone with the secret key can produce a valid HMAC tag
  2. Integrity: Any alteration to the message produces a completely different tag

HMAC also resists length extension attacks (which plague simple H(key || message) constructions) because the key is processed both before and after the inner hash.

Unlike raw hash functions, HMAC requires a key, making it perfect for verifying messages between parties who share a secret—exactly what you need for message authentication codes.

Key Takeaway: HMAC combines a hash function with a secret key using a specific double-hashing construction to provide both message authenticity and integrity, resistant to common cryptographic attacks.