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:

AlgorithmTypeMinimum keyRecommended
HS256HMAC + SHA-256 (shared secret)256 bits / 32 bytes256–512 bits
HS384HMAC + SHA-384384 bits / 48 bytes384–512 bits
HS512HMAC + SHA-512512 bits / 64 bytes512 bits
RS256RSA key pair2048-bit modulus2048–4096 bits
ES256ECDSA, P-256 curveP-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?

Method 1: Generate a JWT Secret in Your Browser

The fastest option, with no terminal needed:

  1. Open the JWT Secret Generator.
  2. Choose HMAC for HS256/HS384/HS512, or RSA / ECDSA for key pairs.
  3. Pick the key size: 256 bits for HS256, 2048 bits or more for RSA.
  4. 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:

# 32 random bytes, Base64-encoded (HS256) openssl rand -base64 32 # 64 random bytes for HS512 openssl rand -base64 64

For an RS256 key pair:

# private key (keep secret) openssl genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:2048 # public key (safe to share with verifiers) openssl pkey -in private.pem -pubout -out public.pem

For an ES256 key pair:

openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out ec-private.pem openssl pkey -in ec-private.pem -pubout -out ec-public.pem

Method 3: Node.js

// HS256 secret — 32 bytes from the OS CSPRNG node -e "console.log(require('crypto').randomBytes(32).toString('base64'))"

Method 4: Python

# secrets uses the OS CSPRNG — never use random.random() for keys python3 -c "import secrets; print(secrets.token_urlsafe(32))"

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

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

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 Generator

FAQ

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.

Related Articles