HS256: one shared secret signs and verifies
HS256 means HMAC using SHA-256. HMAC is a keyed hash: it takes a secret and a message and produces a fixed 32-byte tag that cannot be produced without the secret. In a JWT the message is the ASCII string header.payload, and the tag is the third segment. Verification recomputes the tag with the same secret and compares it with the received one in constant time.
Because the same secret both signs and verifies, HS256 is symmetric. That is convenient for a single service that issues and checks its own tokens, and awkward as soon as a second service needs to verify them. Every verifier must hold the signing secret, and therefore every verifier can mint a token that every other verifier will accept. If one service is compromised, the signing key is compromised for all of them.
The computation, step by step
Take the two encoded segments and join them with a dot. The result is a byte string of ASCII characters. Apply HMAC-SHA256 with the secret as the key. The output is 32 bytes; base64url encode it without padding and append it as the third segment. Nothing else is hashed: the dots inside the JSON, the padding that was removed, the whitespace around the token — none of it is part of the input unless it is literally present in the first two segments.
HMAC is defined as H((K ⊕ opad) || H((K ⊕ ipad) || message)) with the block size of the hash. You do not need to implement it, but the structure explains two properties: a key longer than the block size is hashed first, and changing a single bit of the message changes the whole tag. It also explains why a secret with a trailing newline is a different key.
| Algorithm | Hash | Tag size | Minimum secret |
|---|---|---|---|
| HS256 | SHA-256 | 32 bytes | 32 bytes |
| HS384 | SHA-384 | 48 bytes | 48 bytes |
| HS512 | SHA-512 | 64 bytes | 64 bytes |
Choosing and storing the secret
Generate the secret with a cryptographic random generator, not from a passphrase. Thirty-two random bytes give 256 bits of entropy, which matches the security level of the hash. Store it in a secret manager or an environment variable injected at runtime, not in the repository, not in a container image, and not in a JWT itself. Rotate it on a schedule and after any suspected exposure, with a short overlap so that tokens signed with the old secret remain valid until they expire.
A rotation overlap needs care. If the verifier tries the new secret and then the old one, it must still enforce the algorithm allowlist and the claim checks. It should also bound the overlap to the maximum token lifetime, otherwise an old secret stays useful long after rotation. Log which key version verified a token, never the secret.
Why HMAC verification fails
The most common cause is a secret that differs by bytes rather than by meaning. A value copied from a dashboard may include a trailing newline; a value stored base64-encoded may need decoding; a .env file may quote the value and include the quotes. Compare the byte length and a hash of the secret on both sides during a controlled test. The second most common cause is using the wrong secret for the environment, which is invisible when both are random strings of the same length.
The third is an implementation detail: some libraries expect the secret as text and some as bytes, and a few interpret a base64 string automatically. Mixing conventions between issuer and verifier produces a valid-looking configuration that never verifies. Write down the convention once, in the code that loads the secret, and use it everywhere.
The algorithm-confusion trap
Suppose an API verifies RS256 tokens with a public key. An attacker changes the header to HS256 and signs the token using the public key text as the HMAC secret. If the library follows the token's alg and treats the configured public key as a byte string, the signature verifies. The token is then forged but accepted. The defence is simple: the verifier chooses the algorithm from configuration, checks that the token's alg equals it, and imports the key as the type that algorithm requires.
The same principle applies to key type. An RSA public key and an HMAC secret are both byte strings at the API boundary in some libraries, which is how the confusion happens. A strict implementation never lets a key cross from one algorithm family to another. This site's verifier shows the algorithm explicitly and requires you to choose it, rather than silently following the header.
When HS256 is the right choice
Use HS256 for a single service or a small, fully trusted group where the signing and verifying code is the same deployment, and where distributing one secret to all instances is acceptable. It is fast, simple, and has no key-format ambiguity. Use RS256, ES256 or EdDSA when a third party must verify without being able to sign, when keys must be published in a JWKS, or when the signer is a separate security boundary.
A shared secret is a shared capability. Count every verifier as a potential issuer before choosing HS256.
Sources
- RFC 7518 §3.2 — HMAC with SHA-2
- RFC 2104 — HMAC: Keyed-Hashing for Message Authentication
- OWASP JWT cheat sheet — algorithm confusion
Questions
How long should an HS256 secret be?
At least as long as the hash output: 32 bytes for HS256, 48 for HS384, 64 for HS512. A human-memorable password is not enough. Generate the secret from a cryptographic random source and store it as a secret, because anyone who learns it can mint tokens.
Can I use a public key as an HMAC secret?
Technically yes, and that is exactly the algorithm-confusion attack. If a verifier accepts HS256 while configured for RS256, an attacker can use the public key, which is public, as the HMAC secret and forge a token. Reject algorithms outside an explicit allowlist and check that the key type matches.
Is HS256 weaker than RS256?
Not cryptographically. HMAC-SHA256 is a sound MAC. The difference is key distribution: HS256 requires every verifier to hold the signing secret, so a read-only service can also issue tokens. RS256 lets you distribute a public key and keep the private key in one place.
Sign a sample HS256 token and verify it with the same secret to see the round trip.
Open the verifierWritten against the JOSE specifications (RFC 7515–7519), RFC 7636 and RFC 8037; every example can be reproduced in the verifier linked above.
Back to the guides