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 + messageOutputHMAC-SHA256 tag32-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
SHA-256
SHA-256 is the 256-bit-output member of the SHA-2 family, widely used in TLS, blockchains, and cryptographic applications, with no known practical attacks. GLOSSARY
SHA-512
SHA-512 is the 512-bit-output member of the SHA-2 family; faster than SHA-256 on 64-bit platforms, providing a higher security margin. GLOSSARY
MD5
MD5 is a 128-bit cryptographic hash function designed by Ronald Rivest; proven insecure by collision attacks, it is now used only for non-security purposes such as checksums. GLOSSARY
JWT
JWT (JSON Web Token) is a compact, self-contained token that securely conveys claims as a JSON object, widely used for stateless authentication and information exchange. GLOSSARY