Hash generator · Guide

SHA256 Generator With Key: HMAC-SHA256 Explained

When you need a digest that only someone holding a secret can produce, you want HMAC-SHA256 — not SHA-256 of the secret glued to the message. The difference is not cosmetic; the naive version is breakable.

Why concatenation fails

SHA-256 processes the message block by block and its final state is the digest. Given sha256(secret + message) and the message length, an attacker can resume from that state and compute a valid digest for message + anything without ever knowing the secret. This is the length-extension attack.

HMAC is built specifically to close that hole: it hashes twice with two derived keys, so the output cannot be extended.

Generating an HMAC

Every standard library has it, and the key is passed as a key rather than mixed into the message:

Shell
# shell
printf %s 'message' | openssl dgst -sha256 -hmac 'secret'

# PHP
hash_hmac('sha256', $message, $secret);

# Python
import hmac, hashlib
hmac.new(secret, message, hashlib.sha256).hexdigest()

# Node
crypto.createHmac('sha256', secret).update(message).digest('hex');

Verifying in constant time

Comparing signatures with == leaks timing information: the comparison stops at the first differing byte, and an attacker who can measure that reconstructs the signature byte by byte.

Use the constant-time comparison your language provides — hash_equals() in PHP, hmac.compare_digest() in Python, crypto.timingSafeEqual() in Node. This is the single most-missed step in webhook verification code.

Where you meet it

Webhook signatures from Stripe, GitHub and most SaaS platforms. AWS Signature v4 request signing. JWTs signed with HS256, which is exactly HMAC-SHA256 over the header and payload.

One caveat worth knowing: HMAC proves the sender held the shared secret, so both parties can produce valid signatures. When you need to prove a specific party signed something, you need asymmetric signatures instead.

Frequently asked questions

Is HMAC-SHA256 encryption?

No. It authenticates a message; it does not hide it. Encrypt separately if the content must stay secret.

How long should the key be?

At least 32 bytes of random data for SHA-256. Longer keys are hashed down internally, so there is no benefit past the block size.

Can I use HMAC for passwords?

No. It is fast, like the underlying hash. Password storage needs a slow KDF.

Ready to try it?

Open the free browser-based Hash generator and apply what you just read — no sign-up, runs locally.

Open the Hash generator tool