K056 / Hashes, MACs and key derivation
HMAC
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.
HMAC-SHA-256
See the scope above for fixed examples, reduced parameters and supported variants. Standard operation names and formulas are retained.Output
Previous state
- 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
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.
References
- FIPS 198-1, The Keyed-Hash Message Authentication Code (HMAC) ↗
- Message authentication with HMAC — Go, Python, Node.js, .NET, C++, Java & Rust · cryptoguides.fyi ↗
- Untitled result ↗
- Hash Based Message Authentication ↗
- Message Authentication Codes | CSRC ↗
- Part 2 - Authenticating Messages with Shared Secrets ↗
- hmac — Keyed-Hashing for Message Authentication — Python 3.15.2 documentation ↗
- Security - Geblang Manual ↗