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.
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.
相关文章
SHA-256 vs MD5: When to Use Which Hash Algorithm (2026)
SHA-256 vs MD5 compared: security, speed, and use cases. Learn when MD5 is still acceptable and where you must use SHA-256. Practical decision guide.
Online RSA Encryption/Decryption: Asymmetric Crypto and Digital Signatures
Learn RSA asymmetric encryption algorithm principles and applications. Understand public/private key encryption, digital signatures, key pair generation, and use in HTTPS and API authentication.
Complete Guide to MD5 Hash: How It Works, Uses, and Security
Deep dive into the MD5 algorithm, its common uses, and security vulnerabilities. Learn the differences between MD5, SHA-1, and SHA-256, and when MD5 is still okay to use.