Skip to content
uencoder

HMAC

HMAC (Hash-based Message Authentication Code) is a message authentication code combining a hash function with a secret key, providing integrity and authenticity.

Purpose

Constructs a secure MAC from an existing hash function, requiring no dedicated design.

Mechanism

HMAC(K, m) = H((K ⊕ opad) || H((K ⊕ ipad) || m)), where ipad/opad are fixed pads. Keys longer than the hash block are hashed first. Must be used with a secure hash (SHA-256); even though MD5/SHA-1 are broken as hashes, HMAC-MD5/SHA1 remain comparatively secure.

Use cases

  • API request signing (e.g. AWS SigV4)
  • JWT HS256 signing
  • TLS record MAC
  • Cookie tamper-proofing

Examples

  • Input
    key + message
    Output
    HMAC-SHA256 tag

    32-byte authentication tag

Common errors

Using the bare hash H(key||msg) as a MAC (vulnerable to length-extension attacks)
Always use the HMAC construction

Related terms

Related tools

References