toolqik

05 · Generators

JWT Secret Generator

Correctly sized signing secrets for HS256, HS384 and HS512.

Runs in your browser
Algorithm
Encoding
Secret

  
.env

  

About this tool

A JWT secret is the key your server uses to sign JSON Web Tokens with an HMAC algorithm (HS256, HS384 or HS512), and to check them again later. Anyone who has it can create valid tokens for any user, so it must be long, random and kept private. This generator uses your browser’s cryptographically secure random number generator (crypto.getRandomValues), and the key never leaves the page.

How to use it

  1. Pick the algorithm your app signs with. HS256 is the most common.
  2. Pick an encoding. base64url is compact and safe in .env files and URLs; hex works everywhere.
  3. Optionally raise the key size and set the variable name, then copy the secret or the ready-made .env line.

How long should a JWT secret be?

RFC 7518 (§3.2) requires an HMAC key at least as long as the hash output. The size slider never goes below the minimum for the algorithm you choose.

AlgorithmMinimum keyAs base64urlAs hex
HS25632 bytes (256 bits)43 characters64 characters
HS38448 bytes (384 bits)64 characters96 characters
HS51264 bytes (512 bits)86 characters128 characters

The length must come from random bytes. A 40-character sentence you made up has far less entropy than a 43-character random key, and attackers can test billions of guesses per second offline against any token they capture.

Generate a JWT secret from the command line

Each command prints a 64-byte random secret, suitable for HS256 through HS512:

openssl rand -base64 64

Use the secret to sign and verify tokens

const jwt = require('jsonwebtoken');
const secret = process.env.JWT_SECRET;

const token = jwt.sign({ sub: user.id }, secret, { algorithm: 'HS256', expiresIn: '15m' });

// Always pin the algorithm when verifying
const claims = jwt.verify(token, secret, { algorithms: ['HS256'] });

Pin the accepted algorithm when verifying, as above. Otherwise a token could choose its own algorithm (alg), which has led to real vulnerabilities. You can inspect any token’s header and claims, and check its signature, with the JWT decoder.

Storing and rotating the secret

  • Keep it in an environment variable or a secret manager (AWS Secrets Manager, Vault, Doppler, your host’s settings). Never commit it to Git, and add .env to .gitignore.
  • Use a different secret for development, staging and production.
  • To rotate without logging everyone out, give each key an ID, put it in the token’s kid header, and accept both the old and new keys until the old tokens have expired.
  • If the secret leaks, rotate it immediately: every token signed with it should be treated as forged.

HS256 or RS256?

With HS256 the same secret signs and verifies, which is simple and fast when one service does both. Choose an asymmetric algorithm (RS256, ES256 or EdDSA) when other services or third parties need to verify tokens: they get the public key and can’t create tokens themselves.

FAQ

How do I generate a JWT secret key?

Use a cryptographically secure random generator, like this page or openssl rand -base64 64, to create at least 32 random bytes for HS256 (64 for HS512), then store the result in an environment variable such as JWT_SECRET.

How long should a JWT secret be?

At least as long as the hash output: 32 bytes (256 bits) for HS256, 48 bytes for HS384 and 64 bytes for HS512, as required by RFC 7518.

Can I use a password as my JWT secret?

No. Human-chosen strings have far less entropy than their length suggests and can be brute-forced offline from any token. Use a random key like the ones generated here.

Which encoding should I choose?

Use the encoding your JWT library expects. Most libraries accept the secret as a plain string, so base64url or hex both work. Some expect raw bytes, so decode the value first.

Is HS256 secure?

Yes, with a strong random secret. Use an asymmetric algorithm (RS256, ES256, EdDSA) when other services need to verify tokens without being able to create them.

What happens if my JWT secret leaks?

Anyone with the secret can mint valid tokens for any user. Rotate it immediately, which invalidates all existing tokens, and find out how it leaked.

Is the secret generated on your server?

No. It is generated in your browser with crypto.getRandomValues() and never sent anywhere.

esc
↑↓ navigate↵ open