.env · security🔒 100% client-side — never leaves your browser

JWT Secret Generator for HS256, HS384 and HS512

Generate a JWT secret key with the right length for your algorithm: this JWT secret generator creates 32-, 48- and 64-byte random keys for HS256, HS384 and HS512, base64url-encoded so they paste cleanly into a .env file.

By Aliniyaz Mamat · last reviewed

Be the first to rate
base64url-encoded · crypto.getRandomValues · in-browser only
HS256 (32 bytes)
Minimum for HMAC-SHA256
Secret
.env line
JWT_SECRET=
HS384 (48 bytes)
HMAC-SHA384 — recommended default
Secret
.env line
JWT_SECRET=
HS512 (64 bytes)
Maximum entropy for HMAC-SHA512
Secret
.env line
JWT_SECRET=
CLI alternatives
HS256
openssl rand -base64 32
HS384
openssl rand -base64 48
HS512
openssl rand -base64 64

Key takeaways

  • ✓RFC 7518 requires an HMAC key at least as long as the hash: 32 bytes for HS256, 48 for HS384, 64 for HS512.
  • ✓Output is base64url without padding (A-Z a-z 0-9 - _): 32 bytes become 43 characters, 64 bytes become 86.
  • ✓Never use a password or a word as a JWT secret. A short secret can be brute-forced offline from a single token with tools like hashcat.
  • ✓If one service signs and many only verify, use RS256 or ES256 instead: verifiers then only need the public key.

What it does

  • ▸HS256 (32 bytes), HS384 (48 bytes) and HS512 (64 bytes) presets
  • ▸base64url output, safe in .env files, URLs and headers
  • ▸Ready-to-paste JWT_SECRET= line and the matching openssl command
  • ▸Generated with crypto.getRandomValues
  • ▸Nothing is uploaded or stored

Privacy

Runs 100% in your browser. Your .env never touches our servers.

client-side only

When to use this tool

  • →Signing access tokens in an Express, Fastify, NestJS, Django REST or Spring Boot API
  • →Supabase or Hasura setups that need a JWT secret of a specific length
  • →Replacing a weak secret like secret or changeme found in a code review
  • →Separate secrets per environment and per service

Common mistakes

  • ⚠Using a human-chosen string: my-super-secret falls to a dictionary attack in seconds
  • ⚠Sharing one HMAC secret between many services: every one of them can then mint tokens, not only verify them
  • ⚠Base64-decoding in some services but not others: the same text yields different key bytes. Decide once how the secret is read
  • ⚠Accepting the alg from the token header. Pin the algorithm in your verify call (algorithms: ["HS256"])

How long should a JWT secret be?

AlgorithmMinimum key (RFC 7518)Generatedbase64url length
HS256256 bits32 bytes43 characters
HS384384 bits48 bytes64 characters
HS512512 bits64 bytes86 characters

A longer key than the minimum does not hurt, but it does not add security either: HMAC hashes keys longer than the block size first. Pick the size that matches the algorithm your library uses.

Using the secret in code

Node.js (jose)

import { SignJWT, jwtVerify } from "jose";

const key = new TextEncoder().encode(process.env.JWT_SECRET);

const token = await new SignJWT({ sub: "user_123" })
  .setProtectedHeader({ alg: "HS256" })
  .setIssuedAt()
  .setExpirationTime("15m")
  .sign(key);

const { payload } = await jwtVerify(token, key, { algorithms: ["HS256"] });

Java (jjwt)

// The value is base64url: use the URL decoder, not Decoders.BASE64
SecretKey key = Keys.hmacShaKeyFor(Decoders.BASE64URL.decode(System.getenv("JWT_SECRET")));

Python (PyJWT)

import os, jwt

token = jwt.encode({"sub": "user_123"}, os.environ["JWT_SECRET"], algorithm="HS256")
payload = jwt.decode(token, os.environ["JWT_SECRET"], algorithms=["HS256"])

HS256 vs RS256 vs ES256

HS256 (HMAC)RS256 (RSA)ES256 (ECDSA)
Keyone shared secretprivate + public keyprivate + public key
Who can create tokenseveryone who can verifyonly the private-key holderonly the private-key holder
Token signature size32 bytes256 bytes (2048-bit key)64 bytes
Best forone service signs and verifiesmany verifiers, JWKS endpointssame as RS256, smaller tokens

For a single backend, HS256 with a random 32-byte secret is simple and secure. Once a second team or service needs to verify tokens, move to asymmetric keys so a leak on the verifying side cannot be used to sign. Store the secret like any other: see .env best practices and GitHub Actions secrets.

Frequently asked questions

What is the minimum secure key length for HS256?

256 bits, which is 32 random bytes. RFC 7518 section 3.2 requires the key to be at least as long as the hash output. The HS256 preset generates exactly that.

Should the secret be base64 or plain text?

Both work as long as every service reads it the same way. Most Node.js and Python libraries use the string's UTF-8 bytes directly; Java's jjwt usually decodes it first, so use Decoders.BASE64URL for these values.

Can I use the same secret for staging and production?

No. A token signed in staging would then be valid in production. Use a separate secret per environment and per service.

What happens if my JWT secret leaks?

Anyone can create valid tokens with any claims, including admin roles. Replace the secret immediately; all existing tokens become invalid, so users have to sign in again.

Is the generated secret stored anywhere?

No. It is generated in your browser with crypto.getRandomValues and never sent anywhere. The general secret generator offers more formats (hex, passwords, session secrets).

Related tools

Learn more

Compare

coming soon

Get notified when env syncing launches

We're building a tiny tool to keep .env files in sync across teammates and environments. Leave your email — no spam, just a single launch ping.

We use your address only for this launch email. You can ask us to delete it at any time. Privacy policy