Disclosure: we earn a commission if you buy through the links on this page, at no extra cost to you.
🔑 Secret Key Generator: 256-Bit Keys in the Browser, Nothing Sent
GitGuardian's 2024 State of Secrets Sprawl report found 12.8 million new hardcoded secrets detected in public GitHub commits in a single year, up 28% from the year before. A large share of those secrets were generated with Math.random(), a function that is statistically predictable and unsuitable for cryptographic use. A proper secret key generator produces 256-bit values using your browser's built-in CSPRNG, the keys are assembled locally, and nothing is transmitted to any server.
Why 256 Bits Is the Current Standard
The bit length of a secret key determines how many possible values an attacker must try to guess it by brute force. A 128-bit key has 2128 possible combinations, roughly 340 undecillion. Even with current hardware that is computationally unreachable. A 256-bit key squares that problem: 2256 possible values, a number so large it exceeds estimates of atoms in the observable universe.
NIST SP 800-57 (Recommendation for Key Management) recommends 256-bit keys for symmetric algorithms when targeting 128-bit security strength beyond 2031. The standard states: "For long-term protection of sensitive information, 256-bit keys provide an adequate security margin against advances in computing power, including quantum-based attacks on symmetric algorithms." AES-256 is the most common use case. HMAC-SHA256 and HMAC-SHA512 also benefit from 256-bit or longer key material.
128-bit keys are still acceptable for many use cases today, but 256 bits costs almost nothing extra in computation and future-proofs the key against advances in both classical and quantum computing. When in doubt, generate 256.
Browser-Based Generation: Why It Matters
The safest secret key generator runs entirely in your browser. No request goes to a backend, no value is logged in a web server access file, and no copy persists on infrastructure you do not control. The key exists only in your browser's JavaScript memory and then in your clipboard or a local file.
Server-side generation creates avoidable risk. The key travels over the network at least once, appears in server logs if any logging is active, and exists on a machine with its own attack surface. For a brief window it is exposed to anyone who can read memory or logs on that server.
As OWASP's Cryptographic Storage Cheat Sheet notes: "Keys must be generated using a cryptographically secure random number generator (CSPRNG). Keys must never be transmitted over insecure channels." A browser running crypto.getRandomValues() satisfies both conditions by definition: the key never leaves the device it was created on.
You can verify this yourself. Open the Network panel in browser developer tools before generating a key. After generation, check whether any outbound request contained the key value. A trustworthy browser-based generator produces zero matching network traffic.
CSPRNG vs Math.random(): The Security Gap
JavaScript's Math.random() is a pseudorandom number generator seeded from a small internal state. It is designed for performance, not for security. Modern V8 engine implementations use xorshift128+, an algorithm whose full internal state can be recovered from 128 consecutive outputs. That means an attacker who observes enough outputs can predict all future values and reconstruct past ones.
The crypto.getRandomValues() API is different in every meaningful way. It draws from the operating system's entropy pool, which combines hardware noise, timing jitter, and other sources that an attacker cannot observe. It is the same source used for TLS key generation and operating system-level cryptographic operations. NIST SP 800-133r2 specifies that approved random bit generators must pass statistical tests and be computationally infeasible to predict; crypto.getRandomValues() meets those requirements where Math.random() does not.
The practical difference: a secret key generated with Math.random() can be cracked in seconds on modern hardware if an attacker knows or guesses the seed. A key from crypto.getRandomValues()256 values, which is not feasible.
Use Cases: When You Need a Secret Key
| Use Case | Recommended Key Size | Encoding | Example Format |
|---|---|---|---|
| AES-256 encryption (data at rest) | 256 bits (32 bytes) | Hex or Base64 | a3f8d2... (64 hex chars) |
| AES-128 encryption (transport) | 128 bits (16 bytes) | Hex or Base64 | 7c2e1b... (32 hex chars) |
| HMAC-SHA256 (API request signing) | 256 bits minimum | Hex or Base64 | 64 hex chars or 44 Base64 chars |
| JWT HS256 secret | 256 bits minimum | UTF-8 or Base64 | 32+ byte string |
| API token / bearer token | 128-256 bits | Base64url or hex | 22-43 Base64url chars |
| Database encryption key | 256 bits | Hex | 64 hex chars, stored in KMS |
For JWT secrets specifically, the RFC 7518 specification requires that HMAC-SHA256 keys be at least 256 bits (32 bytes). Many tutorials show short strings like "mysecret" as examples. Those are insecure. A properly generated JWT secret is 32+ bytes of CSPRNG output, encoded as Base64 or stored securely in environment variables.
How to Generate and Store a Secret Key Safely
Generating the key is the easy part. Storing and managing it correctly is where most incidents actually occur.
- Generate in-browser or via a trusted CLI tool. For command-line use,
openssl rand -hex 32produces 256 bits encoded as 64 hex characters.openssl rand -base64 32gives Base64. Both use the system CSPRNG. Browser-based tools usingcrypto.getRandomValues()are equivalent in security. - Never commit keys to source control. Store them as environment variables, in a secrets manager (AWS Secrets Manager, HashiCorp Vault, Azure Key Vault), or in a
.envfile excluded from version control via.gitignore. - Rotate on compromise. Any key that may have been exposed should be replaced immediately. A 256-bit key is cheap to generate; the application downtime for rotation is the only real cost.
- Use separate keys per environment. Development, staging, and production should each have distinct keys. A key leak in a development environment should not affect production data.
- Log key identifiers, not key values. If your application logs operations, log a key ID or fingerprint, never the key itself.
For teams managing multiple secrets, a password manager with secure notes is a practical first step. NordPass stores secrets with XChaCha20 encryption and zero-knowledge architecture, meaning even NordPass cannot read what you store. It is not a full secrets manager, but for small teams it closes the most common gap: secrets stored in plaintext documents or shared over chat.
What to Avoid
Several common shortcuts undermine secret key security.
- Using passwords as keys directly. Human-chosen passwords have far too little entropy to serve as cryptographic keys. If you need to derive a key from a password, use a proper key derivation function: Argon2id (preferred), bcrypt, or PBKDF2 with at least 600,000 iterations of HMAC-SHA256.
- Reusing keys across services. One key per service limits blast radius. If a key for service A is compromised, service B remains protected.
- Generating keys server-side in a shared hosting environment. Shared hosts often run many customers' code on the same machine. Even if generation is technically correct, the key exists briefly in a shared environment.
- Short keys "for performance." AES-256 is only marginally slower than AES-128 on modern hardware with AES-NI instruction sets. The performance difference does not justify the reduction in security margin.
- Storing keys in browser localStorage. Any JavaScript on the page can read localStorage. Keys that need to persist should live in a backend secrets store, not the browser.
Verifying Any Secret Key Generator Before You Use It
Not all tools that claim to generate secret keys are equally trustworthy. Before relying on one, run three checks.
First, test offline. Load the page, disconnect from the internet, and generate a key. If it still works, the generation is genuinely client-side. If it fails or returns an error, some part of the process requires the network, which means your key may be passing through a server.
Second, check the source. Open developer tools, go to the Sources tab, and find the generation function. Confirm it calls window.crypto.getRandomValues() or the equivalent Node.js crypto.randomBytes(). Any use of Math.random(), Math.floor, or timestamp-based seeding is a red flag.
Third, audit the network tab during generation. Watch for any outbound request whose payload or query string contains a value that matches your generated key. A clean generator produces none.
CISA's guidance on software supply chain security also recommends verifying that tools used in development pipelines have published, verifiable build processes. For a secret key generator that runs client-side with open-source JavaScript, this is straightforward: the generation logic is readable in the page source itself.