Random Token Generator: Create Secure Hex and Base64 Tokens
Generate cryptographically secure random tokens in hex or Base64 encoding for API keys, session IDs, secrets, and CSRF tokens — client-side only.
Try the free online tool
Runs entirely in your browser — no signup, no uploads.
Secure random tokens are the foundation of many authentication and authorization mechanisms: API keys, session identifiers, CSRF tokens, password reset links, OAuth state parameters, and more. These tokens must be generated from a cryptographically secure source — if an attacker can predict a token, they can hijack sessions, forge requests, or gain unauthorized API access. Our Random Token Generator produces tokens using the Web Crypto API's `crypto.getRandomValues()`, ensuring true cryptographic unpredictability.
Unlike UUIDs, which have a fixed 128-bit format, random tokens can be generated at any desired bit length — 128, 192, 256, or 512 bits — and encoded as hexadecimal or Base64 for use in different contexts. A 256-bit random token has 2^256 possible values, making brute-force enumeration impossible even with all the computing power on Earth.
This tool lets you specify the token length in bytes, choose hex or Base64 encoding, and generate single or multiple tokens at once. Everything runs in your browser — no tokens are transmitted to any server. Copy generated tokens directly into your configuration files, secrets management system, or development environment.
What Is a Cryptographically Secure Random Token?
A random token is a string of bytes generated from a cryptographically secure pseudorandom number generator (CSPRNG). The key property is unpredictability: given any number of previously generated tokens, an attacker cannot predict the next one. This is distinct from tokens generated by weak random sources like `Math.random()` in JavaScript or `rand()` in C, which are deterministic given the seed and can sometimes be predicted from observable outputs.
Token security is measured in bits of entropy — the log base 2 of the number of possible values. A 16-byte (128-bit) token has 2^128 possible values, a 32-byte (256-bit) token has 2^256 possible values. NIST recommends at least 128 bits of entropy for session tokens and API keys; 256 bits is recommended for long-lived secrets like signing keys.
Tokens are encoded in different formats for different contexts: hex (0-9, a-f) is universal and easy to store in databases; Base64 is more compact (1.33 bytes per character vs 2 bytes for hex) and suitable for HTTP headers and URLs (use the URL-safe Base64 variant for URLs).
How to Use This Tool
Generating a secure token takes just a few seconds.
- 1
Choose the token size
Select the number of bytes (not characters) of random data to generate. 16 bytes gives 128 bits, 32 bytes gives 256 bits. For API keys and session tokens, 32 bytes (256 bits) is a good standard choice.
- 2
Select the encoding
Choose Hex for a string of hexadecimal characters (length = 2 × bytes), or Base64 for a more compact string (length ≈ 1.33 × bytes). Use URL-safe Base64 if the token will appear in URLs or HTTP headers without encoding.
- 3
Set quantity if needed
Specify how many tokens to generate if you need multiple, for example to seed a database with test API keys or pre-generate a batch of one-time codes.
- 4
Click Generate
Press Generate to produce the token(s). Each token is independently drawn from fresh cryptographic entropy — there is no sequential relationship between generated tokens.
- 5
Copy and store securely
Copy the generated token(s) to your clipboard and immediately store them in a secrets manager (HashiCorp Vault, AWS Secrets Manager, environment variables) rather than hardcoding them in source code.
Common Use Cases
Secure random tokens are needed wherever unpredictable secret values are required.
- API key generation for REST and GraphQL APIs, where each client receives a unique, unguessable token to authenticate their requests.
- Session identifier generation for web applications — after login, the server issues a cryptographically random session ID stored in an HttpOnly cookie.
- CSRF token generation to prevent cross-site request forgery — a unique token is embedded in each form and verified server-side on submission.
- Password reset and email verification links — a short-lived, single-use random token is appended to a link emailed to the user.
- OAuth 2.0 state parameter generation to prevent CSRF attacks during the OAuth authorization flow.
Tips and Best Practices
Follow these guidelines to use random tokens securely in your applications.
- Use at least 128 bits (16 bytes) for session tokens and CSRF tokens, and at least 256 bits (32 bytes) for API keys, signing keys, and long-lived secrets.
- Always generate tokens server-side for security-critical applications. Client-side generation is acceptable for non-security identifiers but not for tokens that authenticate users or protect resources.
- Store tokens in a hashed form (SHA-256 of the token) in your database, similar to how passwords should be hashed. If your database is compromised, attackers cannot use the stored hashes directly.
- Set appropriate expiration times on tokens. Session tokens should expire after inactivity. Password reset tokens should expire within 15–60 minutes. API keys should be rotatable and revocable.
- Transmit tokens only over TLS (HTTPS) — never over HTTP. Use the HttpOnly and Secure flags for session cookies to prevent JavaScript access and insecure transmission.
Frequently Asked Questions
How is a random token different from a UUID?
A UUID v4 is a 122-bit random value formatted in a specific 8-4-4-4-12 hexadecimal structure per RFC 4122. A random token is a raw sequence of random bytes encoded as hex or Base64 without any fixed format. Random tokens can be any length, making them more flexible for custom API key or session token formats.
How many bits of entropy do I need for a secure token?
NIST SP 800-63B recommends at least 128 bits for session tokens. For API keys and long-lived secrets, 256 bits is the current best practice. This tool generates tokens up to 512 bits, which provides an extraordinary security margin for any foreseeable use case.
Should I use hex or Base64 for my tokens?
Hex is longer but universally safe in any context (URLs, headers, databases, config files). Base64 is more compact but requires URL-safe encoding if used in URLs. Choose based on where the token will be used — hex for database storage and config files, Base64url for HTTP headers and URL parameters.
Can I use Math.random() to generate tokens in JavaScript?
No. `Math.random()` is a pseudo-random number generator that is not cryptographically secure and should never be used for security tokens. Always use `crypto.getRandomValues()` in browsers or `crypto.randomBytes()` in Node.js for security-sensitive token generation.
How should I store tokens in my database?
Store the SHA-256 hash of the token, not the token itself, similar to how passwords should be hashed. When a user presents a token, hash it and compare against the stored hash. This way, a database breach does not immediately expose valid tokens.
What is the best practice for API key formats?
A common pattern is to include a prefix identifying your service (e.g., 'sk_live_' or 'tok_'), followed by a random token. This makes keys easy to identify in logs and during security audits, and allows tools like GitHub's secret scanning to detect accidentally committed keys. The random portion should be at least 128 bits (32 hex chars or 22 Base64url chars).
Are the tokens generated by this tool safe to use in production?
Yes, for non-server-side generation use cases. The tool uses `crypto.getRandomValues()`, which is the same CSPRNG used by security libraries. However, for production systems, generating tokens server-side using your server's CSPRNG (`crypto.randomBytes()` in Node.js, `secrets.token_hex()` in Python) is the more auditable and standard approach.
Ready to use this tool?
Free, instant, no account required. Runs entirely in your browser.
More Security Tools Guides
Password Generator: Create Cryptographically Secure Passwords Online
5 min read
Password Strength Checker: How to Evaluate and Improve Your Password
5 min read
Hash Generator: Create MD5, SHA-256, and SHA-512 Hashes Online
5 min read
Hash Compare: Verify File and Data Integrity by Comparing Hashes
4 min read