A JSON Web Token is only as strong as the secret that signs it. Most guides show a single command and move on, but three details decide whether the result is safe in production: how many bytes the key really has, how it is encoded when you store it, and where it lives.

1. The size rule is about bytes, not characters

RFC 7518 section 3.2 requires an HMAC key at least as large as the hash output. In practice that means 32 random bytes for HS256, 48 for HS384 and 64 for HS512. Written in hex, those are 64, 96 and 128 characters; in unpadded Base64URL, 43, 64 and 86.

This is where many projects go wrong. A "32-character secret" is 32 bytes only if every character is plain ASCII. If the value is hex, those 32 characters encode just 16 bytes — half the HS256 minimum. Check what your library does: some decode the string before signing, some use the text bytes as-is.

# 32 random bytes = 64 hex characters = the HS256 minimum node -e "console.log(require('crypto').randomBytes(32).toString('hex'))" # decode an existing value and count the bytes node -e "console.log(Buffer.from(process.env.JWT_SECRET, 'hex').length)" 

2. Encoding only changes how the key is written

The same random bytes can be stored as Hex, Base64 or Base64URL. Hex survives every .env parser but doubles the length. Standard Base64 can contain +, / and =, which need quoting in shell scripts and some config formats. Base64URL replaces those characters with - and _, which makes it the safest default for a value that travels through environment files and headers.

Whatever you choose, the signing and verifying services must read it the same way. jsonwebtoken and PyJWT use a string secret as-is, while libraries such as jose expect bytes that you build yourself.

3. Generation is the easy part; storage is the hard part

A secret in git history is a secret that must be rotated, even if you delete the file later. The minimum viable setup is an environment variable loaded from a .env file that is listed in .gitignore, with a committed .env.example that contains only the variable name. Production deserves better: a secret manager such as Vault, AWS Secrets Manager or your platform's encrypted environment store, with access limited to the services that sign and verify tokens.

Plan rotation before you need it. Add a kid (key id) header to new tokens, keep a small map from kid to key on the verifying side, sign new tokens with the new key while old tokens are still accepted, and retire the old key after the longest token lifetime has passed. Access tokens that live 5–15 minutes make this painless; week-long tokens make it painful.

4. If a website generates the key for you, check where it runs

A browser-local generator can be safe, but only if the key never leaves your device. Here is how to check any page in about thirty seconds:

  1. Open DevTools (F12), go to the Network tab, and clear it.
  2. Generate a key on the page.
  3. Look at every request that appears. If a request body or URL contains the key you just generated, the value was sent to a server — close the tab and generate somewhere else.

You can also search the page source for getRandomValues (a browser CSPRNG) and for Math.random (not secure). HTTPS protects data in transit, but it says nothing about what the site does with the key after it arrives.

The short checklist

  • 32, 48 or 64 random bytes for HS256, HS384, HS512 — never a passphrase.
  • Encode before storing; stay consistent on both sides.
  • Keep it in a secret manager, never in the repository, logs or a chat.
  • Rotate with kid before you have to.

I keep a free, sign-up-free generator at https://jwtsecretkeygenerator.org/ — it runs entirely in your browser, shows Hex, Base64 and Base64URL side by side with the RFC minimums, and copies a ready .env line. There is also a decoder and verifier at https://jwtsecretkeygenerator.org/jwt-decoder/ if you want to inspect a token while you are at it.