Random Byte/Hex Generator Online | Secure CSPRNG (Hex, Base64, Binary)

Token Generation
100% client-side. Processed locally in your browser using the Web Crypto API. Nothing is sent to any server.

How It Works

Never use Math.random() for security purposes. This tool uses 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

BytesBitsHex LengthBest for
129624 charsAES-GCM nonces (IVs)
1612832 charsSession tokens, minimum security
3225664 charsAPI keys, AES-256 keys, shared secrets
64512128 charsLong-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

Using Math.random() for security. Math.random() is NOT cryptographically secure — it uses a predictable PRNG algorithm. Always use crypto.getRandomValues() or the Node.js crypto module.
Generating insufficient entropy. 16 bytes (128 bits) is the practical minimum for session tokens. For API keys and encryption keys, use 32 bytes (256 bits) to match AES-256 security level.
Reusing random values. Nonces and IVs must be unique for each encryption operation. Never reuse a nonce with the same key in AES-GCM — this completely breaks confidentiality.
Confusing encoding with security. Hex and Base64 are representation formats, not security mechanisms. The security comes from the randomness of the underlying bytes, not how they are displayed.
Not validating output length. When using random bytes for specific purposes (12 bytes for AES-GCM nonces, 32 bytes for AES-256 keys), verify the output matches the expected length.

Security Notes

CSPRNG is seeded from OS entropy. The browser's crypto.getRandomValues() draws from the operating system's entropy pool — hardware noise, timing jitter, and other unpredictable sources.
Entropy equals byte length × 8. 32 bytes provides 256 bits of entropy, matching the security level of AES-256. This is currently considered unbreakable.
Base64URL for web tokens. Use URL-safe Base64 encoding for API keys and tokens to avoid encoding issues in URLs, query parameters, and HTTP headers.
Fresh on each generation. Every call to crypto.getRandomValues() produces new random bytes. Previous values cannot be predicted from observed outputs.

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

Related Concepts

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

Learn More