HMAC Generator

Generate Hash-based Message Authentication Codes with multiple algorithms and output formats

Algorithm

Output Format

HMAC Result
How It Works
Code Examples

HMAC Hash

About HMAC

HMAC (Hash-based Message Authentication Code) is a specific type of message authentication code (MAC) involving a cryptographic hash function and a secret cryptographic key.

Key Features

  • Message Authentication: Verifies both the data integrity and authenticity of a message
  • Secret Key: Uses a shared secret key for generating and verifying the MAC
  • Cryptographic Hash: Applies a hash function (like SHA-256) to the message and key
  • Security: Resistant to various cryptographic attacks when used with secure hash functions

Algorithm Security Levels

  • Recommended: SHA-256, SHA-384, SHA-512, SHA3-256, SHA3-384, SHA3-512, BLAKE2b
  • Legacy/Deprecated: SHA-1, MD5, RIPEMD-160

When to Use HMAC

  • API request authentication
  • Secure message transmission
  • Data integrity verification
  • Session token generation
  • Password storage (with proper key management)

HMAC Implementation Examples

JavaScript (Web Crypto API)

async function generateHMAC(message, secretKey, algorithm = 'SHA-256') { const encoder = new TextEncoder(); const key = await crypto.subtle.importKey( 'raw', encoder.encode(secretKey), { name: 'HMAC', hash: algorithm }, false, ['sign'] ); const signature = await crypto.subtle.sign( 'HMAC', key, encoder.encode(message) ); // Convert to hex const hashArray = Array.from(new Uint8Array(signature)); return hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); }

Python

import hmac import hashlib def generate_hmac(message, secret_key, algorithm='sha256'): algo_map = { 'sha256': hashlib.sha256, 'sha512': hashlib.sha512, 'sha3_256': hashlib.sha3_256, 'blake2b': lambda: hashlib.blake2b(digest_size=64) } hash_func = algo_map.get(algorithm.lower(), hashlib.sha256) return hmac.new( secret_key.encode('utf-8'), message.encode('utf-8'), hash_func ).hexdigest()

PHP

function generate_hmac($message, $secret_key, $algorithm = 'sha256') { $algo_map = [ 'sha256' => 'sha256', 'sha512' => 'sha512', 'sha3-256' => 'sha3-256', 'blake2b' => 'blake2b' ]; $algo = $algo_map[$algorithm] ?? 'sha256'; return hash_hmac($algo, $message, $secret_key); }

Security Considerations

Common Use Cases