JWT secret generator
Generate a random secret key for signing JSON Web Tokens. 256 bits for HS256, 384 for HS384, 512 for HS512, in hex or base64. It's made in your browser.
Each key has 256 bits of randomness and is 64 characters long.
Keys are generated in your browser with the Web Crypto API and are never sent to a server. Generate a new one before you use it: the keys shown when the page loads came from our server.
How to generate a JWT secret
- Leave the size at 256 bits for HS256, or choose 384 or 512 bits for HS384 or HS512.
- Pick hex or base64url, whichever your library expects.
- Click "Generate new keys" and copy one.
Generate a fresh one before you copy. The keys shown when the page loads came from our server so the page isn't empty, and a signing secret should never have crossed a network.
How long a JWT secret has to be
The size depends on the algorithm. The standard that defines JWT algorithms, RFC 7518, says a key "of the same size as the hash output (for instance, 256 bits for 'HS256') or larger MUST be used".
| Algorithm | Minimum key size | Choose here | Length in hex |
|---|---|---|---|
| HS256 | 256 bits | 256 bits (32 bytes) | 64 characters |
| HS384 | 384 bits | 384 bits (48 bytes) | 96 characters |
| HS512 | 512 bits | 512 bits (64 bytes) | 128 characters |
A longer key than the minimum is allowed and does no harm. A shorter one weakens the signature, and a word or phrase is worse still: token signatures can be attacked offline, by guessing secrets until one produces a matching signature.
What the secret does
A JSON Web Token has three parts: a header, a payload and a signature. With the HS algorithms, the signature is an HMAC of the first two parts, computed with your secret.
Anyone who has the secret can do two things: check that a token is genuine, and create new tokens that pass that check. So the secret has to stay on your servers. If it leaks, an attacker can issue themselves a token for any user.
Using the secret
Keep it out of your code. Put it in an environment variable or a secrets manager and read it at startup.
JWT_SECRET=4f8a...your 64 hex characters...
// Node.js, with the jsonwebtoken package
const jwt = require("jsonwebtoken")
const token = jwt.sign({ sub: "user_123" }, process.env.JWT_SECRET, { algorithm: "HS256", expiresIn: "1h" })
const payload = jwt.verify(token, process.env.JWT_SECRET, { algorithms: ["HS256"] })
Two details in that example matter:
- Name the algorithm when you verify. Passing an explicit list stops a token from choosing its own algorithm
- Set an expiry. The
expclaim is a Unix timestamp. The Unix timestamp converter shows when a given token runs out
A hex secret is used as text by most libraries, which is fine: 64 random hex characters still carry 256 bits. If your library decodes base64 secrets into bytes, generate base64 and tell it so.
From the command line, the same thing is one line:
openssl rand -hex 32
Rotating a JWT secret
Changing the secret invalidates every token signed with the old one, which signs everyone out. To avoid that:
- Add the new secret alongside the old one.
- Sign new tokens with the new secret, and accept both when verifying.
- Remove the old secret once the longest-lived old token has expired.
A kid (key ID) value in the token header tells your code which secret to check against.
HS256 or an asymmetric algorithm?
HS256 uses one shared secret for signing and verifying. That suits a single service that issues and checks its own tokens.
When someone else needs to verify your tokens, use an asymmetric algorithm such as RS256, ES256 or EdDSA. You sign with a private key and publish the public key, so verifiers can check tokens without being able to forge them.
The same idea is how offline software licenses work: the vendor signs a license file with a private key, and the app verifies it with a public key built into it. A shared secret wouldn't do there, because anything shipped inside an app can be extracted. The guide to license keys explains that design, and a licensing API does the signing for you.
For other kinds of secrets, such as API keys and webhook signing keys, see the API key generator.
Common questions about JWT secrets
What is a JWT secret?
The key used to sign and verify JSON Web Tokens with an HMAC algorithm such as HS256. It's a random value known only to your server.
How long should a JWT secret be?
At least 256 bits for HS256, 384 bits for HS384 and 512 bits for HS512. That's 64, 96 and 128 hexadecimal characters.
How do I generate a JWT secret key?
Use the generator above, or run openssl rand -hex 32 for a 256-bit key. Either way, use a cryptographic random source and not a phrase you made up.
Can I use any string as a JWT secret?
A library will accept one, and you shouldn't. A short or guessable secret can be found by brute force from a single captured token, after which an attacker can forge tokens.
Is it safe to generate a JWT secret online?
Only if it's generated on your own machine. This page creates keys in your browser with the Web Crypto API after you click the button, and nothing is sent to a server. For production systems, generating it on the server with OpenSSL is the most cautious choice.
Should the secret be base64 encoded?
It doesn't have to be. What matters is that it contains enough random bits. Use whichever form your JWT library documents.
What happens if my JWT secret leaks?
Anyone holding it can create valid tokens for any user. Replace it immediately, which invalidates existing tokens, and look for tokens issued while it was exposed.
Where should I store a JWT secret?
In an environment variable or a secrets manager on the server. Never in source control, and never in code that runs in a browser or ships inside an app.