SHAGenerator
Security & Algorithms

What Is HMAC?

How HMAC combines secret keys with hash functions (RFC 2104) to prevent length extension attacks and verify message authenticity for webhooks and APIs.

Category: Security & AlgorithmsReading Time: 5 min readPublished: 2026-08-20
IETF RFC 2104 / FIPS 198-1

1. The Problem with Naive Hash Keying

Simply concatenating a secret key and message as `hash(key + message)` is vulnerable to length-extension attacks in Merkle-Damgård algorithms (like SHA-256 and MD5). An attacker can append additional data and compute a valid signature without knowing the secret key.

2. The HMAC Nested Construction (RFC 2104)

HMAC resolves length-extension attacks by using a nested two-pass hashing structure with inner and outer padding constants (ipad and opad):
TEXTSyntax Example
HMAC(K, m) = H((K' ⊕ opad) ∥ H((K' ⊕ ipad) ∥ m))

Where:
• H is the cryptographic hash function (e.g. SHA-256)
• K' is the normalized key derived from secret key K
• ipad = 0x36 repeated to block length (inner pad)
• opad = 0x5c repeated to block length (outer pad)

3. Real-World Webhook and API Security

HMAC-SHA256 is the industry standard for API request signing and webhook security across Stripe, GitHub, Shopify, and AWS Signature Version 4. It guarantees both data integrity and the authentic identity of the sender.

Frequently Asked Questions.

Frequently Asked Questions

Clear answers to common cryptographic and implementation questions.

Why is HMAC-SHA256 preferred over plain SHA-256 for APIs?
Plain SHA-256 proves data wasn’t corrupted, but anyone can calculate it. HMAC proves that the specific holder of the secret key generated the signature.
Can an attacker forge an HMAC without the secret key?
No. Without knowing the secret key, creating a valid HMAC signature is mathematically impossible.

Recommended Tools for this Topic

View all tools →