Random String Generator

Generate random strings for API keys, tokens, IDs and other unique identifiers.

Alphanumeric (A-Z, a-z, 0-9)
Hex (0-9, a-f)
Lowercase letters
Uppercase letters
Numbers only
Alphanumeric + symbols
Base58 (no 0/O/I/l)
URL-safe (A-Z, a-z, 0-9, - and _)
Custom

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

PurposeSuggested lengthCharacter setApproximate entropy
Cache-busting suffix8Alphanumeric~48 bits
Short share code12Alphanumeric~71 bits
Session token32Alphanumeric~190 bits
API key32-40Alphanumeric190-238 bits
Password reset token32URL-safe192 bits
Encryption key material43URL-safe258 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.

Entropy (bits) = length × log₂(character set size)

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

SetCharactersSizeGood for
Hexadecimal0-9 a-f16Hashes, IDs, anything that must be case-insensitive
AlphanumericA-Z a-z 0-962The general-purpose default
URL-safeA-Z a-z 0-9 - _64Tokens appearing in URLs
Base58Alphanumeric without 0 O I l58Anything read aloud or typed by hand
Alphanumeric + symbolsA-Z a-z 0-9 and !@#$%^&*()_+-=76The most entropy per character this tool offers
Drop the ambiguous characters for anything human-facing. 0 and O, 1 and l and I, 5 and S, 8 and B are routinely misread and mistyped. Removing them costs a small amount of entropy per character, which you recover instantly by adding one more character. Voucher codes, device pairing codes and recovery keys should all use an unambiguous set.

Secure versus ordinary randomness

Math.random()crypto.getRandomValues()
Predictable from output?Yes, with enough samplesNo
Suitable for tokens?NoYes
Suitable for test data?YesYes
SpeedMarginally fasterFast 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.

The classic vulnerability. Password reset tokens generated with an ordinary pseudo-random function have been broken repeatedly in real systems. Given enough observed outputs, the generator's internal state can be reconstructed and all future tokens predicted. Always use a cryptographically secure source for anything that must be unguessable.

Handling API keys and secrets

  1. Generate them server-side for production use. A browser tool is fine for development and testing.
  2. Store only a hash of the key, exactly as you would a password, so a database leak does not expose working credentials.
  3. Show the key once at creation and never again.
  4. Prefix keys with an identifier such as sk_live_, which makes accidental leaks detectable by automated secret scanners.
  5. Support rotation by allowing multiple active keys, so a key can be replaced without downtime.
  6. Never commit keys to version control. Use environment variables or a secret manager, and add a pre-commit scanner.
If a key is ever committed to a repository, rotate it. Removing the commit is not enough - it persists in the history, in forks, in clones and in any caching layer that saw it. Public repositories are scanned continuously by automated tools, and exposed keys are typically abused within minutes.
Everything runs in your browser. This tool is implemented entirely in client-side JavaScript. Nothing you enter is transmitted to a server, logged or stored, which matters a great deal for anything security-related. You can verify this yourself by opening your browser's network tab and watching it stay empty while you work.

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.

Related Calculators