Output length by command
| Command | Random bits | Output | Typical use |
|---|---|---|---|
openssl rand -hex 16 | 128 | 32 hex chars | RAILS_MASTER_KEY, short tokens |
openssl rand -base64 16 | 128 | 24 chars | CSRF tokens, nonces |
openssl rand -base64 32 | 256 | 44 chars | AUTH_SECRET, NEXTAUTH_SECRET, JWT secrets |
openssl rand -hex 32 | 256 | 64 hex chars | API keys, webhook secrets, AES-256 keys |
openssl rand -base64 48 | 384 | 64 chars | HS384 JWT secrets |
openssl rand -base64 64 | 512 | 88 chars (2 lines in OpenSSL) | HS512 JWT secrets |
Base64 length is 4 × ceil(N / 3) characters; hex is always 2 × N. More bytes than the algorithm needs do not add security, so 32 bytes covers almost every secret in a .env.
Writing the value into .env from a script
# OpenSSL wraps base64 after 64 characters: strip newlines for long values echo "AUTH_SECRET=$(openssl rand -base64 32)" >> .env echo "JWT_SECRET=$(openssl rand -base64 64 | tr -d '\n')" >> .env echo "API_KEY=$(openssl rand -hex 32)" >> .env # base64url (URL-safe) from base64 openssl rand -base64 32 | tr '+/' '-_' | tr -d '='
No OpenSSL? node -e "console.log(require('crypto').randomBytes(32).toString('base64'))" or python -c "import secrets,base64;print(base64.b64encode(secrets.token_bytes(32)).decode())" produce the same format. For ready-made presets per use case, see the secret generator and the JWT secret generator.
Is browser randomness as good as OpenSSL?
Yes for this purpose. crypto.getRandomValues is required by the Web Crypto specification to use a cryptographically secure generator seeded by the operating system (getrandom() on Linux, BCryptGenRandom on Windows, arc4random on macOS). openssl rand uses OpenSSL's DRBG, which seeds from the same OS source.
What matters more is where the value goes next: straight into your host's secret settings or a password manager, never into chat or a committed file. The guide .env best practices covers the rest.