The short answer: a JWT secret for HS256 must be at least 32 bytes, HS384 at least 48 bytes and HS512 at least 64 bytes, because RFC 7518 section 3.2 requires an HMAC key at least as large as the hash output. In characters that is 64, 96 and 128 hex characters, or 43, 64 and 86 unpadded Base64URL characters. The most common mistake is mixing the two views: 32 characters of hex are only 16 bytes, and 32 random letters and digits carry fewer than 256 bits of entropy. This page gives the exact table, explains why the units differ, and shows how to measure a key you already have.
Minimum JWT secret length by algorithm
| Algorithm | Minimum key | Hex characters | Base64URL characters |
|---|---|---|---|
| HS256 | 32 bytes (256 bits) | 64 | 43 |
| HS384 | 48 bytes (384 bits) | 96 | 64 |
| HS512 | 64 bytes (512 bits) | 128 | 86 |
The rule exists so the key is never weaker than the hash the algorithm uses. A shorter key caps the brute-force resistance of the whole signature, no matter which HMAC variant the token header names. Generate the bytes first, then encode: a 32-byte key is always 64 hex characters and always 43 Base64URL characters, whichever library you use.
Why 32 characters is not always 32 bytes
A byte is not a character. Hex characters encode four bits each, Base64 and Base64URL characters encode six bits each, and only plain ASCII text gives one byte per character. The table below shows what a typical pasted value really means.
| What you store | Characters | Bytes used by HMAC |
|---|---|---|
| Hex-encoded 32 random bytes | 64 | 32 |
| Base64URL-encoded 32 random bytes | 43 | 32 |
| 32 random letters and digits, used as text | 32 | 32 |
| 32 characters that look like hex | 32 | 16 (too short) |
Only the last row is dangerous, and it happens often: someone copies the first 32 characters of a longer hex key. If your framework decodes the value as hex or Base64 before signing, the decoded byte count is what matters. If it uses the string bytes as-is, the ASCII length is the byte count. Decide which convention your library follows and validate the value at startup.
What happens if the secret is too short
An attacker who captures a token can test candidate secrets offline, without touching your server, because the signature is a keyed hash of data they already have. A 16-byte secret leaves 128 bits of search space and a wrong guess is easy to check, while a 32-byte random secret leaves 256 bits. Short or human-chosen secrets are the usual reason a JWT implementation falls to a wordlist attack. There is no server-side rate limit to save you, and no way to tell a brute force from normal traffic until it succeeds.
A too-short key also weakens the HMAC construction itself when it is shorter than the hash output, which is why the RFC sets the minimum rather than leaving it to taste. Keep the key random, keep it out of the token and out of logs, and rotate it if it may have been exposed.
Check the length of a key you already have
# 1. Plain-text secret: ASCII is one byte per character
printf %s "$JWT_SECRET" | wc -c
# 2. Hex-encoded secret: divide the character count by 2
# 64 hex characters = 32 bytes, 128 hex characters = 64 bytes
printf %s "$JWT_SECRET" | wc -c | awk '{print $1 / 2 " bytes"}'
# 3. Base64URL-encoded secret: decode before counting
node -e "console.log(Buffer.from(process.env.JWT_SECRET, 'base64url').length)"
# 4. Hex-encoded secret, decoded in Node.js
node -e "console.log(Buffer.from(process.env.JWT_SECRET, 'hex').length)"
Run the decode that matches your framework: hex, base64 or base64url. A value that passes a character count can still be too short once decoded, and that is the check most people skip.
Generate a key with the right length
# HS256: 32 random bytes, 64 hex characters
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
# HS384: 48 bytes, 96 hex characters
openssl rand -hex 48
# HS512: 64 bytes, 128 hex characters
python3 -c "import secrets; print(secrets.token_hex(64))"
Rather than copying a constant, generate a fresh value for each service and store it where it cannot leak. The generator on this site can produce any of the three sizes as Hex, Base64 or Base64URL and writes a ready .env line.
Frequently asked questions
Is a 32-character JWT secret enough?
It depends on what those characters are. Thirty-two random bytes satisfy HS256, and 32 random bytes are 64 hex characters or 43 Base64URL characters. Thirty-two ASCII letters and digits are 32 bytes as text, which meets the byte minimum but carries about 190 bits of entropy instead of 256. Thirty-two hex characters are only 16 bytes and are below the minimum. When in doubt, call crypto.randomBytes(32) for HS256 and encode the result; do not type a phrase.
Should I use a 128-bit or a 256-bit key?
For HS256, use 256 bits. RFC 7518 section 3.2 requires an HMAC key at least as large as the hash output, so a 128-bit key does not meet the HS256 minimum even though 128 bits is a reasonable floor for other kinds of secrets. A 256-bit key matches the 256-bit hash, a 384-bit key matches HS384 and a 512-bit key matches HS512. Mixing a short key with a large algorithm gains nothing and weakens the security the algorithm is supposed to provide.
Can a JWT secret be longer than 64 bytes?
Yes. The RFC sets a minimum, not a maximum, and the generator on this site accepts keys up to 256 bytes. Very long keys do not add strength beyond the hash output size, because HMAC hashes a key longer than its block size before using it. A key of 32, 48 or 64 random bytes is the practical choice; larger values only make storage and rotation harder. What matters far more is that the key is random and kept secret.
Does the key length have to match the signing algorithm?
Yes, at least the minimum. Use 32 bytes for HS256, 48 for HS384 and 64 for HS512. A 32-byte key with HS512 violates the RFC requirement and weakens the construction, while a 64-byte key with HS256 is allowed but adds no practical security. Some libraries enforce the minimum and raise an error, others do not, so validate the length in your own configuration code and fail closed at startup if it is too short.
How do I check the length of a key I already have?
Count bytes, not characters, and count the decoded value if the key is stored in hex or Base64URL. In a shell, printf %s "$JWT_SECRET" | wc -c counts the characters of a plain-text value; for hex, divide by two, and for Base64URL, decode it first. In Node.js, Buffer.from(value, 'hex').length or Buffer.from(value, 'base64url').length gives the real byte count. A key that is 64 characters but decodes to 16 bytes is too short for every HS algorithm.
Related guides
- Generate a JWT secret in Node.js
- Generate a JWT secret in Python
- This generator vs openssl rand -base64 32
- Where to store a JWT secret and how often to rotate it