Skip to content
uencoder
TASK

Use a cryptographically secure pseudorandom number generator

CSPRNGs produce output indistinguishable from true randomness. Learn the correct APIs and why Math.random() is forbidden in security contexts.

  1. 1

    A CSPRNG collects entropy from hardware sources (thermal noise, keystroke timing, disk seek times) and produces output that is computationally indistinguishable from true randomness. In browsers, use crypto.getRandomValues(); in Node.js, use crypto.randomBytes(). Never use Math.random() — it is a PRNG for simulations, not security.

  2. 2
    Random Bytes Generator →

    Generate cryptographically secure random bytes. This tool uses the platform CSPRNG. Specify the number of bytes needed (32 for a 256-bit key, 16 for a 128-bit key, 12 for a GCM nonce).

  3. 3
    Password Generator →

    Generate a random password using the CSPRNG. Specify length and character set. For high-security passwords, use at least 16 characters with mixed case, digits, and symbols.

  4. 4

    Entropy check: a CSPRNG is only as good as its entropy source. On startup, the OS entropy pool may be low — Node.js and browsers handle this by blocking until enough entropy is collected. On embedded devices or VMs, consider adding hardware entropy sources (e.g., RDRAND, /dev/hwrng).

Frequently asked questions

Why is Math.random() insecure?
Math.random() uses a deterministic algorithm (e.g., xorshift) seeded from a predictable value. With enough outputs, an attacker can reconstruct the internal state and predict all future outputs. It is designed for speed in simulations, not unpredictability for security.
What is entropy?
Entropy is a measure of unpredictability. A CSPRNG needs high-entropy input (from hardware noise) to produce secure output. On a fresh VM, the entropy pool may be small — use haveged or virtio-rng to supplement it. Low entropy leads to predictable "random" output.

Learn more

Want to understand why?

Related tools