HMAC Generator
Part of Encoding & Security Tools
Generate Hash-based Message Authentication Codes using SHA-256, SHA-512, and SHA-1 algorithms for secure data verification.
How to Use the HMAC Generator
- Enter secret key: Provide a secret key that both sender and receiver share. This key must remain confidential.
- Input message: Type or paste the message or data you want to authenticate.
- Select algorithm: Choose SHA-256 (recommended), SHA-512 (more secure), or SHA-1 (legacy support only).
- Generate HMAC: Click "Generate HMAC" to compute the hash-based message authentication code.
- Copy and use: Copy the HMAC hash and send it along with your message for verification.
What is HMAC?
HMAC (Hash-based Message Authentication Code) is a cryptographic algorithm that combines a secret key with a message using a hash function to produce a unique authentication code. Unlike simple hashing with our Hash Generator, HMAC provides both data integrity verification and authentication, ensuring that a message hasn't been altered and confirming the identity of the sender who possesses the secret key.
HMAC is widely used in security protocols including SSL/TLS, IPsec, and JWT tokens. It's the foundation of secure API authentication, where each request includes an HMAC signature that the server can verify. The algorithm is designed to be resistant to collision attacks and length extension attacks that affect simple hash functions.
How HMAC Works
HMAC operates by processing the secret key and message through a cryptographic hash function in a specific way. First, if the key is longer than the hash block size, it's hashed. The key is then padded to the block size and XORed with two different constants (inner and outer padding). The message is concatenated with the inner-padded key and hashed. This intermediate hash is then concatenated with the outer-padded key and hashed again, producing the final HMAC value.
This double-hashing construction provides security guarantees that simple hash(key + message) doesn't offer. Even if an attacker can observe HMACs for many messages, they cannot forge a valid HMAC for a new message without knowing the secret key. The algorithm's security depends entirely on keeping the secret key confidential.
HMAC Algorithm Comparison
SHA-256: Produces a 256-bit (64 character hex) output. This is the current industry standard, offering excellent security and performance. Recommended for most applications including API authentication, secure messaging, and data integrity verification.
SHA-512: Produces a 512-bit (128 character hex) output. Provides even stronger security guarantees and is preferred for extremely sensitive applications or when future-proofing against advances in cryptanalysis. Slightly slower than SHA-256 but still highly efficient.
SHA-1: Produces a 160-bit (40 character hex) output. This algorithm is deprecated for most security purposes due to discovered vulnerabilities. Only use for backward compatibility with legacy systems that require it. Not recommended for new applications.
Common HMAC Use Cases
API Authentication: Many APIs require HMAC signatures to verify that requests come from authorized clients. The client signs each request with a secret key, and the server validates the signature before processing. For JWT tokens, HMAC is commonly used for signature verification.
Data Integrity: HMAC can verify that data hasn't been tampered with during transmission or storage. Any modification to the data will result in a completely different HMAC value.
Secure Cookies: Web applications use HMAC to create tamper-proof cookies. The server signs cookie data and can detect if a user has modified their cookies.
Message Authentication: Ensure that messages come from the claimed sender and haven't been altered in transit. Used extensively in secure communication protocols.
Security Best Practices
Strong secret keys: Use cryptographically random keys of at least 256 bits (32 bytes). Generate secure keys using our Password Generator. Never use passwords or predictable values as HMAC keys without proper key derivation.
Key rotation: Periodically change secret keys, especially if there's any possibility of compromise. Implement a system for distributing new keys securely.
Constant-time comparison: When verifying HMACs, use constant-time comparison functions to prevent timing attacks that could leak information about the secret key.
Use modern algorithms: Prefer SHA-256 or SHA-512. Avoid SHA-1 unless absolutely necessary for compatibility with legacy systems.
HMAC vs Digital Signatures
HMAC and digital signatures both provide authentication, but they work differently. HMAC uses symmetric cryptography—the same secret key is used for both generating and verifying the code. This means both parties must share the secret key securely. Digital signatures use asymmetric cryptography with public/private key pairs, allowing anyone to verify a signature using the public key while only the private key holder can create signatures.
HMAC is typically much faster and simpler to implement, making it ideal for high-performance scenarios like API authentication. Digital signatures provide non-repudiation (proof that a specific party created the signature) but at the cost of computational overhead. Choose HMAC when both parties can securely share a secret, and digital signatures when you need public verifiability or non-repudiation.
Privacy and Security Notice
All HMAC generation is performed entirely in your browser using the Web Crypto API. Your secret keys and messages are never transmitted to our servers or stored anywhere. The data exists only temporarily in your browser's memory. However, we still recommend not using real production keys in any online tool. Always generate HMACs in your own secure environment for production use.