HMAC Functions
HMAC (Hash-based Message Authentication Code) provides message authentication using cryptographic hash functions.
Overview
HMAC combines a secret key with a hash function to create a message authentication code. It's used for:
Message integrity verification
API authentication
Digital signatures
Secure communication protocols
Supported Algorithms
HMAC-SHA-256
SHA-256
256 bits
✅ Recommended
General purpose, API auth
HMAC-SHA-512
SHA-512
512 bits
✅ Recommended
Higher security
HMAC-SHA-1
SHA-1
160 bits
⚠️ Legacy only
Legacy compatibility
HMAC-MD5
MD5
128 bits
⚠️ Legacy only
Legacy compatibility
Basic Usage
Simple HMAC
Output Formats
Input Types
Streaming API
For large data or streaming scenarios:
Streaming with Files
Real-World Examples
API Authentication
Message Integrity
JWT-like Tokens
Security Best Practices
Key Management
Timing-Safe Comparison
Key Derivation
Performance
Sample performance (Linux x64 / Node 20):
HMAC-SHA-256
0.37 M
6.5× faster
General purpose
HMAC-SHA-512
0.41 M
28× faster
Higher security
HMAC-SHA-1
0.49 M
9.2× faster
Legacy compatibility
HMAC-MD5
0.54 M
10.8× faster
Legacy compatibility
Error Handling
TypeScript Support
API Reference
Function Signature
Types
Available Functions
crypto.hmac.sha1(data, options)crypto.hmac.sha256(data, options)crypto.hmac.sha512(data, options)crypto.hmac.md5(data, options)
Streaming API
Timing-Safe Comparison
This function compares two strings/buffers in a timing-safe manner to prevent timing attacks.
Last updated