About this tool
Password-reset links, email verification, invitations, unsubscribe links, magic sign-in links and session IDs all depend on one thing: a token nobody can guess. This generator creates them from your browser’s cryptographically secure random number generator, in the encoding your system expects.
Choosing an encoding
| Encoding | 256-bit length | Good for |
|---|---|---|
| Base64URL | 43 characters | URLs, cookies and headers: the most compact URL-safe choice |
| Hex | 64 characters | Logs, databases and anything case-insensitive |
| Base62 | 43 characters | Letters and digits only; safe to double-click and select |
| Base32 | 52 characters | Case-insensitive systems and codes people type |
| Base64 | 44 characters | Config files; contains + and /, so not for URLs |
Using tokens safely
- 128 bits is the minimum for anything that grants access; 256 bits costs nothing extra.
- Store a hash, not the token. Save
SHA-256(token)in your database and compare hashes, so a database leak doesn’t leak working links. - Expire and use once. Reset and verification tokens should expire within an hour or so and be deleted after use.
- Prefixes help. A prefix such as
reset_identifies the token’s purpose in logs and helps secret scanners spot leaks.
Secure tokens in code
import { randomBytes } from 'node:crypto';
randomBytes(32).toString('base64url'); // 43 chars, 256 bits
randomBytes(32).toString('hex'); // 64 charsimport secrets
secrets.token_urlsafe(32) # 43 chars, 256 bits
secrets.token_hex(32) # 64 charsb := make([]byte, 32)
if _, err := rand.Read(b); err != nil { // crypto/rand
panic(err)
}
token := base64.RawURLEncoding.EncodeToString(b)FAQ
How long should a secure token be?
At least 128 bits of randomness; 256 bits is the common choice and the default here.
Which encoding should I use for URLs?
Base64URL: it is compact and uses only letters, digits, - and _, which need no escaping in URLs.
Should I store tokens in my database?
Store a SHA-256 hash of the token instead. When the user returns, hash the token they present and compare.
Is this different from a UUID?
A UUID v4 has 122 random bits in a fixed format. A secure token can have more randomness and a more compact encoding, which suits secrets better.