Random Byte/Hex Generator Online | Secure CSPRNG (Hex, Base64, Binary)
Token Generation How It Works
crypto.getRandomValues() — the browser's built-in CSPRNG seeded from the OS entropy pool. Math.random() is predictable and must never be used for keys, tokens, or secrets.
What Is a CSPRNG?
A Cryptographically Secure Pseudo-Random Number Generator (CSPRNG) produces random values that are unpredictable and uniformly distributed. The Web Crypto API's crypto.getRandomValues() is seeded from the operating system's entropy pool (hardware noise, timing jitter, etc.), making it suitable for generating cryptographic keys, session tokens, nonces, and initialization vectors.
When to Use Random Bytes
- API keys & secrets — generate 32-byte (256-bit) random tokens for authenticating API requests
- Encryption keys — create raw key material for AES-256 (32 bytes) or AES-128 (16 bytes)
- Initialization vectors (IVs/nonces) — generate unique random values for AES-GCM (12 bytes)
- Session tokens — create unpredictable session identifiers for web applications
- Salts for password hashing — generate unique salts for bcrypt, Argon2, or PBKDF2
- OAuth state parameters — prevent CSRF attacks with random state tokens
Byte Length Recommendations
| Bytes | Bits | Hex Length | Best for |
|---|---|---|---|
| 12 | 96 | 24 chars | AES-GCM nonces (IVs) |
| 16 | 128 | 32 chars | Session tokens, minimum security |
| 32 | 256 | 64 chars | API keys, AES-256 keys, shared secrets |
| 64 | 512 | 128 chars | Long-lived master secrets |
Standards Compliance
- Web Crypto API — W3C standard for browser-native CSPRNG via
crypto.getRandomValues() - NIST SP 800-90A — Recommendation for Random Number Generation Using Deterministic Random Bit Generators
- RFC 4086 — Randomness Requirements for Security
- OWASP — recommends CSPRNG for all security-sensitive random value generation
Last updated:
Code Examples
JavaScript (Browser — Web Crypto API)
// Generate 32 random bytes
const bytes = new Uint8Array(32);
crypto.getRandomValues(bytes);
// Convert to hex
const hex = Array.from(bytes)
.map(b => b.toString(16).padStart(2, '0'))
.join('');
console.log(hex); // 64 hex characters
// Convert to Base64
const base64 = btoa(String.fromCharCode(...bytes));
console.log(base64); // 44 characters
// URL-safe Base64
const urlSafe = base64.replace(/+/g, '-').replace(///g, '_').replace(/=+$/, '');
console.log(urlSafe); Python
import secrets
import base64
# 32 random bytes
random_bytes = secrets.token_bytes(32)
# Hex output
print(f'Hex: {random_bytes.hex()}')
# Base64 output
print(f'Base64: {base64.b64encode(random_bytes).decode()}')
# URL-safe Base64
print(f'URL-safe: {base64.urlsafe_b64encode(random_bytes).decode().rstrip("=")}') Go
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
)
func main() {
bytes := make([]byte, 32)
_, err := rand.Read(bytes)
if err != nil {
panic(err)
}
fmt.Println("Hex:", hex.EncodeToString(bytes))
} Rust
use rand::RngCore;
fn main() {
let mut bytes = [0u8; 32];
rand::thread_rng().fill_bytes(&mut bytes);
let hex_str: String = bytes.iter()
.map(|b| format!("{:02x}", b))
.collect();
println!("Hex: {}", hex_str);
} Node.js
const crypto = require('crypto');
// Generate 32 random bytes
const bytes = crypto.randomBytes(32);
console.log('Hex:', bytes.toString('hex'));
console.log('Base64:', bytes.toString('base64'));
console.log('URL-safe:',
bytes.toString('base64')
.replace(/+/g, '-')
.replace(///g, '_')
.replace(/=+$/, '')
); Common Pitfalls
Security Notes
Disclaimer
This tool is provided for educational and development purposes. Random byte generation uses the browser's CSPRNG, but always follow NIST guidelines and consult a security professional for production deployments. All generation occurs entirely in your browser — no data is transmitted.
Further Reading
- RFC 4086 — Randomness Requirements for Security
- NIST SP 800-90A — Random Number Generation
- MDN — crypto.getRandomValues()
- OWASP Cryptographic Storage Cheat Sheet
Related Concepts
- Key Management — Secure Key Generation & Storage
- Password Generator — Human-Readable Secrets
- BIP39 Mnemonic — Random Entropy to Seed Phrases
- Random Byte Generator
Related Tools
Need human-readable secrets? Try the Passphrase Generator. For password creation, see the Password Generator. To validate BIP39 mnemonics generated from random entropy, use the BIP39 Mnemonic Validator.
Frequently Asked Questions
Are these random bytes cryptographically secure?
Yes. The tool uses crypto.getRandomValues(), the browser built-in CSPRNG, which is seeded from the operating system entropy pool. This is the same source of randomness used for TLS session keys — not to be confused with Math.random(), which is predictable and must never be used for security purposes.
What is the difference between hex, Base64, and binary output?
They are different encodings of the exact same random bytes. Hex uses two characters per byte (easy to read and paste into code), Base64 packs three bytes into four characters (about 25% more compact), and binary shows the raw bits. A 32-byte value is 64 hex characters, roughly 43 Base64 characters, or 256 binary digits.
How many bytes should I generate?
It depends on the use case: 16 bytes (128 bits) is the practical minimum for session tokens; 32 bytes (256 bits) matches AES-256 keys and is the recommended size for API keys and shared secrets; 64 bytes (512 bits) suits long-lived master secrets. Entropy always equals 8 × byte length.
Can I use this tool to generate API keys or secret tokens?
Yes. Generating 32 bytes in Base64URL gives you a URL-safe, high-entropy token in the same style as secrets issued by GitHub, Stripe, or AWS. For secrets that must be typed or memorized by humans, consider the Passphrase Generator instead.
Is anything logged or sent to a server?
No. Generation runs entirely in your browser using the Web Crypto API — no values, lengths, or usage patterns are stored or transmitted. Once the page is cached, the tool even works fully offline.
Privacy: This tool runs entirely in your browser — nothing is uploaded or stored. · Disclaimer: For educational purposes only. Not professional security advice.
More in Tokens
OTP Token Generator
Generate TOTP (time-based) and HOTP (counter-based) one-time passwords online. SHA-1/256/512, 6-8 digits, Google Authenticator compatible.
BIP39 Mnemonic Generator & Validator
Generate or validate BIP39 mnemonic seed phrases online. Derive multi-chain wallet addresses (18 blockchains) via BIP32/BIP44 and SLIP-0010.