What is hmac.mdx?

HMAC is a specific type of message authentication code that uses cryptographic hash functions to verify both the integrity and authenticity of a message. It’s a security mechanism that ensures data hasn’t been tampered with and comes from a trusted source. HMAC combines a secret key with a message using a hash function like SHA-256 to create a unique code that can be verified by others who have the same key.

Let's break it down

Think of HMAC like a special seal on an envelope. When you want to send a secure message, you take your message and a secret password (key), then use a mathematical process (hash function) to create a unique fingerprint. This fingerprint is attached to your message. The recipient uses the same password and mathematical process to verify that the fingerprint matches - proving the message is authentic and hasn’t been changed during delivery.

Why does it matter?

HMAC matters because it solves two critical security problems: ensuring messages haven’t been altered and confirming they come from legitimate sources. Without HMAC, attackers could modify data in transit or impersonate trusted senders. It’s especially important for APIs, digital signatures, and any system where you need to trust that information is genuine and unchanged.

Where is it used?

HMAC is used in web APIs for authentication, digital signatures for documents, secure messaging systems, and password verification. Popular applications include GitHub webhooks, AWS API requests, JWT tokens, and online banking systems. It’s also used in network protocols like IPsec and TLS to ensure secure communications between computers.

Good things about it

HMAC is fast to compute and verify, works with existing hash functions, and provides strong security guarantees when implemented correctly. It’s standardized and widely supported across programming languages and platforms. HMAC doesn’t require complex encryption algorithms, making it easier to implement and debug. It also allows for secure authentication without transmitting passwords directly.

Not-so-good things

HMAC requires both parties to share a secret key, which can be challenging to distribute and manage securely. If the secret key is compromised, all HMACs created with that key become invalid. It’s also vulnerable to replay attacks unless combined with timestamps or nonces. HMAC doesn’t provide confidentiality - it only verifies authenticity and integrity - so messages must be encrypted separately if privacy is needed.