Which secret format for which variable
| Variable | Recommended preset | Why |
|---|---|---|
JWT_SECRET (HS256) | JWT 48 bytes | ≥ 32 bytes required for HS256; more is fine |
JWT_SECRET (HS512) | JWT 64 bytes | HS512 needs ≥ 64 bytes |
SESSION_SECRET, COOKIE_SECRET | Session 32 bytes | Express/Fastify sign cookies with HMAC-SHA256 |
API_KEY for your own services | API key hex 32 bytes | URL- and header-safe, easy to compare in logs |
DB_PASSWORD, REDIS_PASSWORD | Password 32 chars, or no-symbols 16+ | symbols need quoting in URLs and .env |
ENCRYPTION_KEY (AES-256) | API key hex 32 bytes | exactly 32 bytes = 256-bit key |
Framework-specific formats have their own pages: NextAuth AUTH_SECRET, Django SECRET_KEY, Laravel APP_KEY, Rails SECRET_KEY_BASE and WordPress salts.
How strong is strong enough?
| Preset | Random bits | Length |
|---|---|---|
| API key hex, 16 bytes | 128 | 32 characters |
| Session secret, 32 bytes | 256 | 43 characters |
| API key hex, 32 bytes | 256 | 64 characters |
| JWT secret, 48 bytes | 384 | 64 characters |
| JWT secret, 64 bytes | 512 | 86 characters |
| Password, 20 chars with symbols (88-char alphabet) | about 129 | 20 characters |
128 random bits cannot be brute-forced with any foreseeable hardware; 256 bits adds margin and matches what HMAC-SHA256 and AES-256 expect. Length beyond that mostly costs readability.
Generating secrets on the command line
openssl rand -hex 32 # 64 hex chars, 256 bits
openssl rand -base64 32 # 44 chars, 256 bits
node -e "console.log(require('crypto').randomBytes(32).toString('base64url'))"
python -c "import secrets; print(secrets.token_urlsafe(32))"All of these use the operating system's CSPRNG, like this page. The OpenSSL rand generator shows the exact output format of each openssl rand variant. Before a release, scan your .env for weak or placeholder values with the ENV leak checker.