JSONHint

Preparing your workspace…

What is HMAC? Understanding Hash-Based Message Authentication

June 2026 · 6 min read · Security, Encoding, Basics

JH
JSONHint
By JSONHint · Practical guide

What is HMAC?

HMAC stands for Hash-based Message Authentication Code. It is a specific construction for creating a Message Authentication Code (MAC) using a cryptographic hash function combined with a secret key. HMAC is defined in RFC 2104 and is widely used across the internet to verify both the data integrity and authenticity of a message.

Unlike a plain cryptographic hash (like SHA-256), which only proves that data hasn't changed, an HMAC proves that the message was created by someone who possesses the shared secret key. This makes HMAC the backbone of API authentication, webhook verification, and secure cookie signing.

How HMAC Works

The HMAC construction uses this formula:

HMAC(K, m) = H((K' XOR opad) || H((K' XOR ipad) || m))

In simpler terms:

  1. The message (m) and a secret key (K) are combined using XOR operations with inner and outer padding constants.
  2. The result is hashed twice using the underlying hash function (H) — typically SHA-256, SHA-384, or SHA-512.
  3. The final output is a fixed-length authentication code (e.g., 64 hex characters for HMAC-SHA256).

The double-hashing construction prevents length extension attacks — a vulnerability present in plain SHA-2 hashes.

HMAC vs Plain Hashing

FeaturePlain Hash (SHA-256)HMAC-SHA256
Verifies data integrity✅ Yes✅ Yes
Verifies sender identity❌ No✅ Yes (requires secret key)
Requires a secret key❌ No✅ Yes
Resistant to length extension attacks❌ No✅ Yes
Output length256 bits (64 hex chars)256 bits (64 hex chars)

Real-World Use Cases

1. API Request Authentication

Services like AWS, Stripe, and Twilio use HMAC-SHA256 to sign API requests. The client signs the request body (or a canonical request string) with a shared secret and sends the signature in an HTTP header. The server independently computes the same HMAC and compares it, rejecting any request where the signatures don't match — preventing tampering and replay attacks.

2. Webhook Verification

GitHub, Stripe, and Shopify sign webhook payloads with HMAC-SHA256 using a shared secret configured in your webhook settings. When you receive a webhook, you compute your own HMAC of the raw request body and compare it with the signature in the X-Hub-Signature-256 header (or equivalent). If they match, the webhook is authentic.

3. JWT Signing (HS256/HS384/HS512)

The HS256, HS384, and HS512 JWT signing algorithms are all HMAC variants. When you sign a JWT with HS256, the header + payload are HMAC-SHA256'd using your secret. The receiver verifies the JWT by recomputing the HMAC. Importantly, this is symmetric — both parties use the same secret key (unlike RSA, which is asymmetric).

4. Cookie Integrity Verification

Web frameworks sign session cookies with HMAC to prevent tampering. Flask (Python) and Rails both use HMAC-SHA256 to sign their session cookies. An attacker who modifies the cookie value cannot forge a valid HMAC without the server's secret key.

Example: Computing HMAC-SHA256

// JavaScript (Node.js)
const crypto = require('crypto');

const secret = 'my-super-secret-key';
const message = '{"user":"alice","amount":99.99}';

const hmac = crypto
  .createHmac('sha256', secret)
  .update(message)
  .digest('hex');

console.log(hmac);
// Output: a1b2c3d4... (64 hex characters)

Generate HMAC Online

Use the JSONHint HMAC Generator to instantly compute HMAC-SHA256, HMAC-SHA384, or HMAC-SHA512 for any message and secret key. All computation runs in your browser using the native Web Crypto API — your secret key is never transmitted anywhere.

Related Tools