MAC (message authentication code) reference
A MAC binds a message to a secret key, producing a short tag that proves the message is both intact and from someone holding the key. MACs protect API tokens, cookies, JWTs and the integrity half of authenticated encryption. This reference compares HMAC, GMAC, Poly1305, CMAC and KMAC by construction type, key size, tag length and the AEAD scheme each is paired with.
How it works
MACs come in three families:
- Hash-based (HMAC) — wraps a hash like SHA-256 in a keyed nested construction. Secure even on length-extendable hashes.
- Cipher-based (CMAC, GMAC) — built from a block cipher such as AES. GMAC is the authentication part of GCM.
- Polynomial (Poly1305) — evaluates a polynomial over a prime field; very fast and used in ChaCha20-Poly1305.
To stay secure you must verify the tag with a constant-time comparison, and for one-time MACs (Poly1305, GMAC) you must never reuse the nonce/one-time key, or an attacker can forge tags.
Tips and notes
- For standalone authentication, HMAC-SHA-256 is the safe default.
- For authenticated encryption, use AES-GCM (GMAC) or ChaCha20-Poly1305.
- Truncating a 256-bit HMAC tag to 128 bits is acceptable; shorter tags weaken forgery resistance.
- Always compare tags in constant time and never reuse nonces with one-time MACs.