Every JWT your app issues is only as strong as the key that signs it. A weak secret like mysecret or changeme123 can be cracked offline in minutes from a single captured token. Then an attacker can mint tokens for any user, admin included.
This guide covers how long your secret needs to be, which algorithm to choose, and four safe ways to generate one: OpenSSL, Node.js, Python, and a free generator that runs in your browser.
Quick answer: for HS256, generate 32 random bytes (256 bits) with a cryptographically secure generator and store them as Base64 in an environment variable. openssl rand -base64 32 does exactly that. Or use the JWT Secret Generator, which runs locally in your browser.
How Long Should a JWT Secret Be?
For HMAC algorithms, RFC 7518 §3.2 says the key must be at least as long as the hash output:
| Algorithm | Type | Minimum key | Recommended |
|---|---|---|---|
HS256 | HMAC + SHA-256 (shared secret) | 256 bits / 32 bytes | 256–512 bits |
HS384 | HMAC + SHA-384 | 384 bits / 48 bytes | 384–512 bits |
HS512 | HMAC + SHA-512 | 512 bits / 64 bytes | 512 bits |
RS256 | RSA key pair | 2048-bit modulus | 2048–4096 bits |
ES256 | ECDSA, P-256 curve | P-256 (fixed) | P-256 |
The key point is that length only matters if the bytes are random. A 40-character password typed by a human has far less entropy than 32 bytes from a secure random generator. Never derive a JWT secret from a password, a username, your app name or a UUID.
HS256 vs RS256 vs ES256: Which Should You Use?
- HS256 (symmetric): one shared secret both signs and verifies tokens. It's simple and fast. Use it when the same backend issues and checks tokens.
- RS256 (asymmetric): a private key signs tokens and a public key verifies them. Use it when other services, partners or an API gateway need to verify tokens. They get the public key only, so they can't forge tokens.
- ES256 (asymmetric): same model as RS256 but with elliptic curves. Keys and signatures are much smaller at similar strength. It's a good default for new asymmetric setups if your libraries support it.
Method 1: Generate a JWT Secret in Your Browser
The fastest option, with no terminal needed:
- Open the JWT Secret Generator.
- Choose HMAC for HS256/HS384/HS512, or RSA / ECDSA for key pairs.
- Pick the key size: 256 bits for HS256, 2048 bits or more for RSA.
- Click generate, then copy the key into your environment variable or secret manager.
Privacy: the generator uses your browser's built-in Web Crypto API (crypto.getRandomValues and crypto.subtle.generateKey). The key is created on your device and never sent to any server. Avoid online generators that create keys server-side.
Method 2: OpenSSL (macOS, Linux, WSL)
For an HS256 secret:
For an RS256 key pair:
For an ES256 key pair:
Method 3: Node.js
Method 4: Python
Don't use Math.random(), Python's random module, timestamps, UUIDs or hashes of predictable strings. None of them are cryptographically secure.
Where to Store Your JWT Secret
- Environment variables: e.g.
JWT_SECRET=..., loaded at startup. Keep.envfiles out of git by adding them to.gitignore. - A secret manager for production: AWS Secrets Manager, GCP Secret Manager, HashiCorp Vault, Doppler, or your platform's encrypted variables.
- Never hard-code it in source, commit it to a repo, ship it to the frontend, or log it. If a secret ever lands in git history, treat it as leaked and rotate it. Deleting the file isn't enough.
Rotating Keys Without Logging Everyone Out
Add a kid (key ID) to the JWT header. During rotation, sign new tokens with the new key but keep verifying with both keys until the old tokens have expired. Then retire the old key. With short-lived access tokens (5–15 minutes) plus refresh tokens, a rotation completes in minutes.
Quick Checklist
- ✅ 256+ bits from a cryptographically secure generator
- ✅ Algorithm pinned on verification (reject
alg: noneand unexpected algorithms) - ✅ Stored in env vars or a secret manager, never in the repo
- ✅ Short
expon access tokens - ✅ A rotation plan using
kid
Once your tokens are being issued, you can inspect them with the JWT Decoder to check claims and expiry. For how the header, payload and signature fit together, see What Is a JWT and How Do You Decode One?
Generate a JWT Secret Key — Free & Private
HMAC, RSA and ECDSA keys, generated locally with Web Crypto. Nothing leaves your browser.
Open JWT Secret GeneratorFAQ
How long should a JWT secret key be?
For HS256, use at least 256 bits (32 random bytes). RFC 7518 requires the HMAC key to be at least as long as the hash output, so HS384 needs 384 bits and HS512 needs 512 bits.
Can I use a password as my JWT secret?
No. A human-chosen password has far less randomness than its length suggests and can be brute-forced offline from any token you issue. Generate the secret with a cryptographically secure random generator instead.
Should I use HS256 or RS256?
Use HS256 when a single service both issues and verifies tokens. Use RS256 or ES256 when other services need to verify tokens — they only get the public key, so they cannot mint tokens.
Is it safe to generate a JWT secret online?
Only if the generator runs entirely in your browser. EazyStudio's JWT Secret Generator uses the Web Crypto API locally — the key is never sent to a server.
How often should I rotate my JWT secret?
Rotate on a schedule (for example every 90 days) and immediately if you suspect a leak. Use a "kid" (key ID) header so old and new keys can be valid during the switch-over.