← Cryptography guide

K056 / Hashes, MACs and key derivation

HMAC

HMAC message authentication · Survey date 2026-12-05

HMAC is a keyed message authentication code based on a cryptographic hash function.

CRYPTOGRAPHY LAB / K056

Cryptography Lab · HMAC

Run real computations and inspect intermediate states step by step.

Educational only. Never enter real passwords or secret keys. Computation stays in your browser; input and trace values are not sent to a server. Do not use this implementation in production.

Scope: Limited implementation

HMAC-SHA-256

See the scope above for fixed examples, reduced parameters and supported variants. Standard operation names and formulas are retained.

Reference ↗

Open lab →

Definition and overview
HMAC is a keyed message authentication code based on a cryptographic hash function. Source document ↗
Classification and reversibility
MAC/authentication code, not encryption; verify by recomputing and comparing. Source document ↗
Key or secret requirements
Requires a shared secret key. Source document ↗
Operating principle and structure
Pads key, XORs with ipad and combines with message for hashing. Source document ↗
Key size, block size and parameters
Hash-specific block size B (e.g. 64); parameters include key, digest and tag lengths. Source document ↗
Security strength and recommendation
Included among NIST-approved general-purpose MAC algorithms. Source document ↗
Known attacks and vulnerabilities
Simple secret-prefix hashing is vulnerable to length extension; use HMAC-like construction. Source document ↗
Standards and certification
Specified in FIPS 198-1 and related standards. Source document ↗
Typical uses
Webhook/API request signatures, cookie signing and download integrity checks. Source document ↗
Implementations, libraries and tools
Python hmac and other libraries implement RFC 2104. Source document ↗
Decryption or decoding
No decryption; recompute tag with same key and compare in constant time. Source document ↗
Advantages
Combines iterative cryptographic hashes with shared secret keys. Source document ↗
Limitations and cautions
No confidentiality; requires separate encryption. Source document ↗
Distinguishing encryption, encoding and hashing
Distinguish HMAC from encryption, encoding and ordinary hashing; it verifies authentication tags. Source document ↗

Practical examples

Examples are illustrative; they are not execution-tested implementations or operational recommendations.

Example

Sender computes T from secret K and message M, sends M/T. Receiver computes T' with shared K and compares in constant time to check integrity and origin.

Procedure

Generate tag with high-entropy key and message; send. Recompute using same key and received message, constant-time compare. No decryption/decoding; use AES-GCM or other encryption for confidentiality.

Scenario

Use for webhook verification, API signatures, cookies and downloads; combine with separate AEAD if confidentiality is needed.

Pitfalls and alternatives

Avoid ordinary == comparison due to timing leakage; use constant-time comparison. Use high-entropy keys, not passwords. HMAC does not provide confidentiality; manage keys securely.

Diagram source ↗

References