A secure token is a string of unguessable random data used to authenticate or identify a request — session identifiers, CSRF tokens, API keys, password-reset links, and email-verification codes all rely on them. The single most important property is that the value must come from a cryptographically secure random source. This tool generates tokens using the browser’s Web Crypto API, so every value is suitable for real authentication systems, and nothing ever leaves your device.
How it works
The generator draws random bytes directly from crypto.getRandomValues, the browser’s cryptographically secure pseudo-random number generator (CSPRNG). It then encodes those raw bytes into a text form you can paste into code:
- Allocate a
Uint8Arrayof the byte length you chose. - Fill it with
crypto.getRandomValues(bytes). - Encode the bytes as hex, standard base64, or URL-safe base64url.
Critically, this never uses Math.random, which is predictable and must never be used for anything security-sensitive. A token built from 32 secure random bytes has 256 bits of entropy — far beyond brute-force reach.
Encodings and sizes
- Hex doubles the byte count in characters (32 bytes = 64 hex chars) and is the easiest to read, but least compact.
- Base64 is compact (about 4 characters per 3 bytes) and standard for HTTP headers and cookies.
- Base64url swaps
+/for-_and removes=padding so the token is safe to embed directly in URLs and filenames without percent-encoding.
Choosing the right byte length
| Use case | Recommended bytes | Why |
|---|---|---|
| CSRF tokens | 32 bytes | 256-bit space; standard recommendation |
| Session IDs | 32 bytes | Same — cookie or header transport |
| Password-reset links | 32–48 bytes | Should expire quickly; 32 is fine |
| Long-lived API keys | 32–64 bytes | More bytes provides deeper margin |
| Low-risk identifiers | 16 bytes | 128-bit entropy; adequate for most |
Going below 16 bytes risks collision in large-scale systems. Going above 64 bytes has no practical security benefit and wastes storage.
Common mistakes
Using Math.random. This is not cryptographically secure. Node.js’s crypto.randomBytes, Python’s secrets.token_hex, and the browser’s crypto.getRandomValues are correct; Math.random is never acceptable for security-sensitive tokens.
Storing tokens in plaintext. Password-reset tokens and one-time codes stored in plain text in a database can be read by anyone with database access. Hash them with SHA-256 before storage — the link carries the raw token, the database holds the hash.
Reusing tokens. Each token should be single-use. Mark reset and verification tokens as used the moment they are consumed, so replaying an intercepted token does nothing.
For session and anti-CSRF tokens, 32 bytes is a strong, conventional choice. Always store generated secrets in a secure secret manager, never in source control, and rotate them if exposure is suspected.