What Is HMAC
HMAC (Hash-based Message Authentication Code) is an algorithm that uses a cryptographic hash function and a secret key to verify both the integrity and authenticity of a message.
In simple terms, HMAC is a "keyed hash" — only someone with the same key can generate or verify an HMAC value. Try generating one yourself with our HMAC generator: paste a message and secret key, and the signature appears instantly.
HMAC vs Regular Hashing
| Feature | Regular Hash (e.g., SHA-256) | HMAC | |---------|------------------------------|------| | Input | Message only | Message + Key | | Verifiable without key | ✅ | ❌ | | Tamper-resistant | ❌ (anyone can compute) | ✅ (requires key) | | Typical use | File integrity, password storage | API signing, message auth |
How HMAC Works
The HMAC computation formula:
HMAC(K, m) = H((K' ⊕ opad) || H((K' ⊕ ipad) || m))
Where:
K= Keym= MessageH= Hash function (e.g., SHA-256)K'= Processed key (hashed if too long, padded if too short)opad= Outer padding (0x5c repeated)ipad= Inner padding (0x36 repeated)
How to Use an Online HMAC Generator
Using DevToolkit Pro's HMAC Generator:
- Enter your secret key
- Enter the message to sign
- Choose the hash algorithm (SHA-256 / SHA-512 / SHA-1 / MD5)
- Click Generate
- Copy the resulting HMAC value
Common HMAC Use Cases
1. API Request Signing
Many APIs (AWS, Stripe, WeChat Pay) require HMAC-signed requests:
Authorization: HMAC-SHA256 signature=abc123...
The server computes HMAC with the same key over request parameters and compares it to the client-submitted signature to verify the request hasn't been tampered with.
2. Webhook Verification
GitHub, Stripe, and other platforms verify webhooks using HMAC-SHA256:
const crypto = require('crypto');
function verifyWebhook(payload, signature, secret) {
const expected = crypto
.createHmac('sha256', secret)
.update(payload)
.digest('hex');
return crypto.timingSafeEqual(
Buffer.from(signature),
Buffer.from(expected)
);
}
3. JWT Signing
JWT's HS256 algorithm is essentially HMAC-SHA256:
header.payload.signature
where signature = HMAC-SHA256(base64(header) + "." + base64(payload), secret)
4. Message Integrity Verification
When transmitting messages over untrusted networks, appending an HMAC ensures the message hasn't been altered in transit.
HMAC Key Security Best Practices
- Key length: At least 256 bits (32 bytes); use cryptographically secure random generation
- Key storage: Never hardcode in source code; use environment variables or key management services
- Key rotation: Rotate keys periodically and reject requests signed with expired keys
- Timestamp for replay prevention: Include timestamps in signed messages and reject expired ones
FAQ
What's the Difference Between HMAC and Digital Signatures?
HMAC uses symmetric keys (same key for generation and verification); digital signatures use asymmetric keys (private key signs, public key verifies). HMAC is faster but can't prove signature origin to third parties. Choose based on whether non-repudiation is needed.
Which Hash Algorithm Should I Use?
HMAC-SHA256 is the recommended standard. SHA-1 and MD5 are considered insecure and shouldn't be used in new projects.
Can HMAC Prevent Replay Attacks?
Not alone. HMAC only verifies message integrity, not freshness. Combine it with timestamps (timestamp parameter) or nonces to prevent replay attacks.
This article is brought to you by DevToolkit Pro. More developer tools at the homepage.
relatedTools
Related Articles
SHA-256 Hash Algorithm: How It Works, Applications, and Online Tools
Understand SHA-256 internals, comparison with MD5/SHA-1, and real-world applications in password storage, blockchain, and file integrity.
Online Bcrypt Hash Tool: The Gold Standard for Password Hashing
Learn how Bcrypt password hashing works. Understand cost factors, salt generation, and how to use an online tool to generate and verify Bcrypt hashes.
JWT vs Session Cookies: Authentication Comparison and Selection Guide
Sessions are server-side state, JWT is a stateless token. Compare them on microservices fit, SPA compatibility, revocation, and security. Includes common JWT pitfalls and a decision framework.