Random String Generator
Generate random strings for API keys, tokens, IDs and other unique identifiers.
What random strings are for
Random strings are the raw material of a great deal of software: API keys, session tokens, password reset links, file names, database identifiers, test fixtures and cache-busting suffixes. What they all need is unpredictability - the whole value of a token is that nobody can guess it.
This generator uses the browser's cryptographically secure random source, which makes the output suitable for security purposes as well as for test data.
Key takeaways
- Entropy = length × log₂(character set size); aim for 128 bits or more for secrets.
- Use a cryptographically secure source for anything security-related.
- A 32-character alphanumeric string gives about 190 bits - comfortably strong.
- Removing ambiguous characters helps for anything a human will read aloud or type.
- Longer and simpler beats shorter and more exotic.
Choosing a length
| Purpose | Suggested length | Character set | Approximate entropy |
|---|---|---|---|
| Cache-busting suffix | 8 | Alphanumeric | ~48 bits |
| Short share code | 12 | Alphanumeric | ~71 bits |
| Session token | 32 | Alphanumeric | ~190 bits |
| API key | 32-40 | Alphanumeric | 190-238 bits |
| Password reset token | 32 | URL-safe | 192 bits |
| Encryption key material | 43 | URL-safe | 258 bits (covers a 256-bit key) |
128 bits of entropy is the usual floor for anything that must resist attack. There is little practical benefit above 256.
Alphanumeric (62 characters) ≈ 5.95 bits per character
Hexadecimal (16 characters) = 4 bits per character
URL-safe (64 characters) = 6 bits per character
Alphanumeric + symbols (76 characters) ≈ 6.25 bits per character
Choosing a character set
| Set | Characters | Size | Good for |
|---|---|---|---|
| Hexadecimal | 0-9 a-f | 16 | Hashes, IDs, anything that must be case-insensitive |
| Alphanumeric | A-Z a-z 0-9 | 62 | The general-purpose default |
| URL-safe | A-Z a-z 0-9 - _ | 64 | Tokens appearing in URLs |
| Base58 | Alphanumeric without 0 O I l | 58 | Anything read aloud or typed by hand |
| Alphanumeric + symbols | A-Z a-z 0-9 and !@#$%^&*()_+-= | 76 | The most entropy per character this tool offers |
Secure versus ordinary randomness
| Math.random() | crypto.getRandomValues() | |
|---|---|---|
| Predictable from output? | Yes, with enough samples | No |
| Suitable for tokens? | No | Yes |
| Suitable for test data? | Yes | Yes |
| Speed | Marginally faster | Fast enough for any realistic use |
This generator uses the secure source. The performance difference is irrelevant at these volumes, so there is no reason to use the weaker option.
Handling API keys and secrets
- Generate them server-side for production use. A browser tool is fine for development and testing.
- Store only a hash of the key, exactly as you would a password, so a database leak does not expose working credentials.
- Show the key once at creation and never again.
- Prefix keys with an identifier such as
sk_live_, which makes accidental leaks detectable by automated secret scanners. - Support rotation by allowing multiple active keys, so a key can be replaced without downtime.
- Never commit keys to version control. Use environment variables or a secret manager, and add a pre-commit scanner.
Frequently Asked Questions
How long should a random string be?
It depends on the purpose. Around 8 characters is fine for a cache-busting suffix, while tokens and API keys should be 32 characters or more - roughly 190 bits of entropy for an alphanumeric set. The usual floor for anything security-related is 128 bits.
Are these strings cryptographically secure?
Yes. The generator uses the browser's cryptographically secure random source rather than an ordinary pseudo-random function, so the output cannot be predicted from previously generated values.
What is the difference between Math.random and a secure random source?
Math.random is a pseudo-random generator whose internal state can be reconstructed from enough observed outputs, making all future values predictable. A cryptographically secure source is specifically designed to resist that. Use the secure one for anything that must be unguessable.
Which character set should I use?
Alphanumeric is a sensible default. Use URL-safe characters for tokens appearing in links, hexadecimal where case-insensitivity matters, and an unambiguous set that excludes 0, O, 1, l and I for anything a person will read aloud or type by hand.
Can I use this to generate API keys?
For development and testing, yes. Production keys should be generated server-side, stored only as a hash, shown once at creation, and prefixed with an identifier so automated scanners can detect accidental leaks.
What should I do if I accidentally commit an API key?
Rotate it immediately. Removing the commit is not sufficient - the key persists in the repository history, in every fork and clone, and in caching layers. Public repositories are scanned continuously and exposed keys are typically abused within minutes.
Why exclude characters like 0 and O?
Because they are routinely confused when read aloud or typed by hand, along with 1, l and I. Excluding them costs a little entropy per character, which you recover by adding one more character. Worth doing for voucher codes, pairing codes and recovery keys.
Are the generated strings stored anywhere?
No. Generation happens entirely in your browser and nothing is transmitted or recorded. Refreshing the page discards everything.