HMAC Generator Online | SHA-256, SHA-384, SHA-512 Message Authentication

Hash Functions
100% client-side. Processed locally in your browser using the Web Crypto API. Nothing is sent to any server.

How It Works

Never hardcode HMAC secrets in client-side code. The secret key must be kept confidential — use environment variables or server-side secret management. When verifying HMAC, always use constant-time comparison to prevent timing attacks.

What Is HMAC?

HMAC (Hash-based Message Authentication Code) is a mechanism for verifying both the integrity and authenticity of a message using a cryptographic hash function and a shared secret key. Unlike a plain hash, only someone who knows the secret key can produce or verify the authentication tag. HMAC is used in API authentication, webhook verification (Stripe, GitHub, Slack), JWT tokens, and secure message exchange.

When to Use HMAC

  • API request signing — authenticate requests by including an HMAC signature that only the client and server can verify
  • Webhook verification — Stripe, GitHub, and Slack send HMAC signatures with webhook payloads to verify authenticity
  • JWT tokens — HMAC-SHA256 (HS256) is the default algorithm for signing JSON Web Tokens
  • Message authentication — verify that a message came from a trusted source and hasn't been tampered with
  • Session verification — sign session tokens to detect tampering without storing server-side state

HMAC Algorithm Reference

AlgorithmOutput SizeHex LengthBase64 LengthBest for
HMAC-SHA256256-bit (32 bytes)64 chars44 charsAPI authentication, JWT, webhooks
HMAC-SHA384384-bit (48 bytes)96 chars64 charsHigh-security applications
HMAC-SHA512512-bit (64 bytes)128 chars88 charsMaximum security margin

Standards Compliance

  • RFC 2104 — HMAC: Keyed-Hashing for Message Authentication
  • RFC 4231 — Identifiers and Test Vectors for HMAC-SHA-224, HMAC-SHA-256, HMAC-SHA-384, and HMAC-SHA-512
  • NIST FIPS 198-1 — The Keyed-Hash Message Authentication Code (HMAC)
  • Web Crypto API — W3C standard for browser-native HMAC via crypto.subtle.sign()

Last updated:

Code Examples

JavaScript (Browser — Web Crypto API)

// HMAC-SHA256
const key = await crypto.subtle.importKey(
  'raw',
  new TextEncoder().encode('secret-key'),
  { name: 'HMAC', hash: 'SHA-256' },
  false,
  ['sign']
);

const message = new TextEncoder().encode('hello world');
const signature = await crypto.subtle.sign('HMAC', key, message);
const hmac = Array.from(new Uint8Array(signature))
  .map(b => b.toString(16).padStart(2, '0')).join('');
console.log(hmac);

Python

import hmac
import hashlib

key = b'secret-key'
message = b'hello world'

# HMAC-SHA256
signature = hmac.new(key, message, hashlib.sha256).hexdigest()
print(f'HMAC-SHA256: {signature}')

# HMAC-SHA512
signature_512 = hmac.new(key, message, hashlib.sha512).hexdigest()
print(f'HMAC-SHA512: {signature_512}')

Go

package main

import (
    "crypto/hmac"
    "crypto/sha256"
    "encoding/hex"
    "fmt"
)

func main() {
    key := []byte("secret-key")
    message := []byte("hello world")

    mac := hmac.New(sha256.New, key)
    mac.Write(message)
    signature := mac.Sum(nil)

    fmt.Println("HMAC-SHA256:", hex.EncodeToString(signature))
}

Rust

use hmac::{Hmac, Mac};
use sha2::Sha256;

type HmacSha256 = Hmac<Sha256>;

fn main() {
    let key = b"secret-key";
    let message = b"hello world";

    let mut mac = HmacSha256::new_from_slice(key)
        .expect("HMAC can take key of any size");
    mac.update(message);
    let result = mac.finalize();

    println!("HMAC-SHA256: {:x}", result.into_bytes());
}

Node.js

const crypto = require('crypto');

const key = 'secret-key';
const message = 'hello world';

const hmac = crypto.createHmac('sha256', key)
  .update(message)
  .digest('hex');
console.log('HMAC-SHA256:', hmac);

Common Pitfalls

Using non-constant-time comparison. When verifying HMAC, use timing-safe comparison to prevent timing attacks. In Node.js, use crypto.timingSafeEqual(). In Python, use hmac.compare_digest().
Hardcoding secrets in client-side code. HMAC secrets must be kept confidential. Never embed them in JavaScript, mobile apps, or any client-accessible code. Use server-side environment variables or secret management.
Using weak or short keys. HMAC keys should be at least as long as the hash output (32 bytes for SHA-256). Short keys are vulnerable to brute-force attacks.
Confusing HMAC with encryption. HMAC provides integrity and authenticity, not confidentiality. The message is not encrypted — anyone can read it. HMAC only proves who sent it and that it hasn't been modified.
Using HMAC-SHA1 for new systems. While HMAC-SHA1 is not broken in the HMAC construction, SHA-1 itself is deprecated. Use HMAC-SHA256 or HMAC-SHA512 for all new applications.

Security Notes

HMAC-SHA256 is the standard. Most APIs and protocols expect this algorithm. Use SHA-384/512 only when higher security margins are needed.
Rotate keys periodically. For long-lived systems, implement key rotation to limit exposure if a key is compromised. Most APIs support multiple active keys during transition.
HMAC provides both integrity and authenticity. Unlike a plain hash, HMAC proves the message came from someone who knows the secret key — providing authentication in addition to integrity.
Use for webhook verification. Stripe, GitHub, Slack, and other services use HMAC signatures to verify webhook payloads. Always verify the HMAC before processing the payload.

Disclaimer

This tool is provided for educational and development purposes. HMAC is a critical security mechanism — always follow RFC 2104 guidelines and consult a security professional for production deployments. All computation occurs entirely in your browser.

Further Reading

Related Concepts

Related Tools

Need to generate a plain hash without a key? Try the SHA Hash Generator. For signing full JWT tokens with HMAC, see the JWT Signer & Verifier. You can also derive keys from passwords using the KDF Hash Generator.

Frequently Asked Questions

What is the difference between HMAC and a regular hash?

A regular hash (like SHA-256) only verifies data integrity — anyone can compute it. HMAC adds a secret key, so only someone who knows the key can produce or verify the authentication tag. This provides both integrity and authenticity.

When should I use HMAC?

HMAC is used when you need to verify that a message came from a trusted source and hasn't been tampered with. Common uses include API authentication (signing requests), JWT tokens, session verification, webhook signatures (Stripe, GitHub, Slack), and secure message exchange between parties.

Which HMAC algorithm should I use?

HMAC-SHA256 is the standard choice and what most APIs expect. HMAC-SHA384 and HMAC-SHA512 offer larger outputs for higher security margins. Avoid HMAC-SHA1 for new systems as SHA-1 is no longer considered secure.

What is the difference between hex and Base64 output?

Both encode the same HMAC bytes. Hex (lowercase) produces a 64-character string for SHA-256, while Base64 produces a 44-character string. Choose based on your integration requirements — GitHub webhooks use hex, while some other services prefer Base64.

How does HMAC verification work?

Verification recomputes the HMAC using the same message, key, and algorithm, then compares it to the expected signature using constant-time comparison to prevent timing attacks. If they match, the message is authentic and untampered.

Privacy: This tool runs entirely in your browser — nothing is uploaded or stored. · Disclaimer: For educational purposes only. Not professional security advice.

More in Hash

Learn More