Generate and store a secret key

API key, signing secret, encryption key: where it must come from, where it should live, and how to replace it when the time comes.

A secret key is randomness and nothing else

An API key, the secret that signs JWTs, an AES encryption key, Django's SECRET_KEY or Laravel's APP_KEY have one thing in common: their only protection is being impossible to guess. Unlike a password, nobody needs to remember or type them. So they can be long and perfectly random, and they should be.

Their strength is measured in bytes of randomness: 16 bytes make 128 bits, 32 bytes make 256 bits. At 128 bits, no exhaustive search is conceivable; 256 bits is the usual value in modern tools and leaves a comfortable margin.

Why a cryptographic generator

Not all random generators are equal. Functions designed for games or simulations, such as Math.random() in JavaScript or rand() in C, produce sequences that look random but can be predicted from a few observed values. A key derived from the date, a project name or the hash of a sentence can be recovered with a little method.

A key must come from a cryptographically secure random number generator, fed by the operating system. The following commands use one:

# 32 bytes in base64 (44 characters)
openssl rand -base64 32

# 32 bytes in hexadecimal (64 characters)
openssl rand -hex 32

# Python: 32 bytes in base64url
python3 -c "import secrets; print(secrets.token_urlsafe(32))"

# Laravel: writes APP_KEY to the .env file
php artisan key:generate

# Django
python -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())"

In the browser, crypto.getRandomValues plays this role: the kit's secret key generator uses it, and offers presets for the most common formats, with the line ready to paste into your configuration.

Size and encoding: what your tool expects

The same 32-byte key can be written in several ways: 64 characters in hexadecimal, 44 in base64 (with a trailing =), 43 in base64url without padding. The encoding does not change the key's strength, only how it is written. A few reference points:

  • JWT HS256 / HS512: RFC 7518 requires a key at least as long as the hash output, that is 32 bytes for HS256 and 64 bytes for HS512.
  • AES-128 / AES-256: exactly 16 or 32 bytes. Here the size is not a minimum but a constraint of the algorithm.
  • Laravel: APP_KEY=base64: followed by 32 bytes encoded in base64, for the default AES-256-CBC cipher.
  • Django: a 50-character string, as produced by get_random_secret_key().

One point deserves attention: depending on the library, a key supplied as text is either decoded (the hexadecimal turns back into 32 bytes) or used as is (the 64 characters become 64 bytes). Both work, but not the same way from one program to another: check the documentation for what your tool expects, especially when two applications have to share the same key.

Where to store it

A secret key lives outside the source code. The Git repository is copied to every developer's machine, into continuous integration, sometimes to contractors; and whatever goes into it stays in the history, even after the file is deleted. Suitable places:

  • Environment variables on the server or container, possibly loaded from a .env file listed in .gitignore.
  • Platform secrets: those of your CI tool, your hosting provider or a dedicated vault (Azure Key Vault, AWS Secrets Manager, HashiCorp Vault…).
  • For local development, the mechanisms your framework provides, such as .NET User Secrets, which keep the key outside the project folder.

Two rules round off storage: one key per purpose (one to sign tokens, another to encrypt) and one key per environment (development, test, production). That way, a key exposed in test leaves production untouched, and each replacement stays contained.

If a key has been committed, treat it as exposed: remove it from the code and, above all, replace it. Cleaning the history does not reach copies of the repository already made.

Rotate it

Replacing a key is called rotation. You do it after someone leaves, after an incident or a possible exposure, or simply at regular intervals. What it involves depends on the use:

  • API key: most providers let you have two active keys at once. Create the new one, deploy it, check it, then revoke the old one.
  • JWT signing secret: tokens signed with the old secret become invalid. For a smooth transition, many libraries accept several keys, told apart by the kid identifier.
  • Django: changing SECRET_KEY invalidates sessions and password reset links; since Django 4.1, SECRET_KEY_FALLBACKS keeps the old key accepted during the transition.
  • Laravel: changing APP_KEY makes data encrypted with the old key unreadable; recent versions accept APP_PREVIOUS_KEYS to keep decrypting it.
  • Data encryption key: the data must be re-encrypted with the new key, or the old one kept for reading only.

A rotation prepared in advance (a written procedure, two keys accepted during the transition) goes smoothly on the day it becomes urgent.

Share it with a colleague

Sometimes you have to hand a key to someone: a developer setting up their environment, a contractor deploying, a colleague taking over a service. A ticket, the team chat or an email keep it indefinitely, readable by anyone with access.

The generator's “Send with onlyU” button passes the line ready to paste (for example APP_KEY=base64:…) to onlyU, tab to tab in your browser, without putting it in an address or routing it through the kit's server. onlyU encrypts it in your browser and creates a link your colleague opens only once, valid for 24 hours, after which the link disappears. It is free, with no sign-up.

Your colleague then stores the key where it belongs, in the platform's secrets. If you are sharing HTTP access credentials rather than a key, the guide Basic Auth explained shows why they deserve the same care; for server access, see how to create an SSH key.