HMAC
A way of combining a secret key with a hash function to produce a tag that shows a message came from someone holding the key and was not changed.
In more detail
HMAC stands for hash-based message authentication code. The sender and receiver share one secret key; the receiver recomputes the tag and accepts the message only if the tags match, so an attacker without the key cannot forge one. Because both sides hold the same key, either could have made any valid tag, so an HMAC cannot prove to a third party who sent a message. Compare tags with a constant-time function so timing does not leak information.
Examples
In the APIs course: The course service signs its tokens with HS256, which is HMAC using SHA-256, and webhook receivers commonly check an HMAC of the body.hmac.new(KEY, msg, "sha256").hexdigest()hmac.compare_digest(good, tag)The course uses HMAC to keep its signing demonstration self-contained; real artefact signing uses public-key signatures so verifiers never hold the signing key.Related terms
- digital signature — A value computed from data and a secret key that lets anyone with the matching verification key confirm the data came from the key holder and was not changed.
- SHA-256 — A widely used hash function that turns any data into a 256-bit fingerprint, shown as 64 hexadecimal characters.
- webhook signature — A signature, usually an HMAC of the request body made with a shared secret, that proves a webhook delivery came from the real provider and was not altered.
- JSON Web Token — A compact signed token made of three base64url parts separated by dots: a header, a payload of claims and a signature.
- encryption in transit — Protecting data while it moves across a network, so it cannot be read (confidentiality) or altered unnoticed (integrity) on the way.
- MAC — A short tag computed from a message and a secret key, which lets the receiver check the message is unchanged and came from a key holder.
- keyed hash — A hash computed with a secret key mixed in, so only someone who holds the key can produce or check the correct value.
- constant-time comparison — Comparing two secret values in a way that always takes the same time, whether they differ in the first byte or the last.
- non-repudiation — Proof strong enough that the sender of a message or the author of an action cannot credibly deny it later.
- symmetric encryption — Encryption where the same secret key both encrypts and decrypts, as with AES.
- signed cookie — A cookie whose value carries a signature, such as an HMAC, so the server can detect any change made by the client.
- hash — A short fixed-length fingerprint calculated from data, which changes completely if even one byte of the data changes.
- secret — Any value that grants access, such as a password, access token or private key, and must be kept out of code and version control.
- Web Crypto API — The standard Web API for cryptography, such as hashing, HMAC signing and encryption, available in browsers, Node.js and the Edge runtime.
- secret key — A value known only to the parties allowed to use it, needed to create or check an HMAC or to encrypt and decrypt data.
- crypto.timingSafeEqual() — A node:crypto function that compares two Buffers of equal length in a time that does not depend on where they differ.
- webhook — An HTTP request a provider sends to an address you registered, to tell you an event happened, instead of you polling for it.
Taught in
- JSON Web Tokens — Modern API Architecture: REST, GraphQL & gRPC
- Webhooks Versus Polling — Modern API Architecture: REST, GraphQL & gRPC
- Wikis, Release Notes And Integrations — Microsoft Azure DevOps Engineer Expert (AZ-400) Exam Prep
- Two Reminders, One Booking: Integration And DevOps Tools — CompTIA Cloud+ (CV0-004) Exam Prep
- CIA Triad, Non-Repudiation & Parkerian Hexad — Cybersecurity Fundamentals, GRC & Cryptography
- Cryptographic Hashing: SHA-256, SHA-3 & Passwords — Cybersecurity Fundamentals, GRC & Cryptography
- Data States: Security at Rest, in Transit, and in Use — Cybersecurity Fundamentals, GRC & Cryptography
- JWT Verification: Asymmetric Keys & Refresh Rotation — Production Full-Stack Engineering
- Session Management: Secure Cookies & Token Validation — Next.js 15 & Modern App Router Architecture
- Cryptographic Primitives: node:crypto & Ciphers — Node.js Internals & Backend Services
- Authentication Versus Authorisation — Application Security & Threat Modeling for Engineers
- Cross-Site Request Forgery — Application Security & Threat Modeling for Engineers
- Provenance And Signing — Application Security & Threat Modeling for Engineers
- Identity: Joiners, Federation And MFA — CompTIA Security+ (SY0-801) Exam Prep
- Three Audit Findings And A Phone: Password Policy, Lockouts And MFA — CompTIA Linux+ (XK0-006) Exam Prep
- The Certificate That Expired On A Sunday: Hashes, Encryption And Certificates — CompTIA Linux+ (XK0-006) Exam Prep
References
- RFC 2104: HMAC: Keyed-Hashing for Message Authentication
- GitHub Docs: Validating webhook deliveries
- FIPS 198-1: The Keyed-Hash Message Authentication Code (HMAC)
- MDN glossary: HMAC
- Node.js: Crypto (crypto.createHmac)
- Python docs: hmac