JWT HMAC minimum key sizes

HS256 needs at least 32 bytes, HS384 at least 48 bytes, and HS512 at least 64 bytes. Use cryptographically random bytes. RFC 7518 §3.2 requires an HMAC key at least as large as the hash output.

Random key sizes and encoded lengths
AlgorithmRandom bytesHex charactersBase64url, unpadded
HS256326443
HS384489664
HS5126412886

Characters and randomness are different things

A long human-chosen string does not have the entropy of uniformly random bytes. You cannot calculate its security from character count alone. Generate the bytes with a cryptographic random number generator, then encode them for storage.

Use the same representation when signing and verifying

This tool returns base64url text. Some libraries take that text as a UTF-8 secret; others expect decoded key bytes. Either side must interpret the key in the same way. A text representation and the bytes it encodes are different HMAC keys.

For example, Node can decode this tool's result using Buffer.from(process.env.JWT_SECRET, 'base64url') if your library expects raw bytes. Do not change an existing application's interpretation without planning key rotation.

What if my library rejects the key length?

Check the selected algorithm, the actual bytes passed to the library and whether an encoding was decoded. Adding predictable characters to a weak secret may pass a length check without making it secure. Generate a new random key and rotate it deliberately.

When to use asymmetric signing

With HMAC, every service that can verify a token also has the key needed to sign one. If verifiers must not be able to issue tokens, use an asymmetric algorithm supported by your stack, such as RS256 or EdDSA, and distribute only the public key.

Keep signing keys in a secret manager, explicitly restrict accepted algorithms and validate expected issuer and audience claims. See RFC 8725, JWT Best Current Practices.