Skip to content
LearnCryptographically secure random number generator

What is a CSPRNG (secure random number generator)?

Also known asCSPRNGcrypto.getRandomValues
Example. Kiteline is a fictional company.
On this page

A cryptographically secure random number generator (CSPRNG) is a source of random values designed so that nobody, even after seeing any amount of its earlier output, can predict the next value or work backwards to previous ones better than by guessing, which makes it suitable for generating keys, tokens and passwords.

Why ordinary randomness isn't enough

Most programming languages have two kinds of random function. One is fast and statistically even, meant for simulations, games and shuffling; its output can often be predicted from a few earlier values. The other is built for security. MDN marks the difference for JavaScript: Math.random is "a non-cryptographic source of random numbers", while crypto.getRandomValues() "lets you get cryptographically strong random values" (MDN).

A secret generated with the wrong one may look random and still be guessable. Unlike a weak password, nothing warns you.

How a CSPRNG works

Real randomness from hardware events is slow and limited, so systems collect it, mix it into a seed and expand that seed with a cryptographic algorithm. MDN describes browsers the same way: implementations use "a pseudo-random number generator seeded with a value with enough entropy", and the algorithm "may vary across user agents, but is suitable for cryptographic purposes." The quality of the result depends on the seed's entropy and on the algorithm resisting prediction.

Which APIs to use

  • Browsers: crypto.getRandomValues(). MDN also advises preferring generateKey() when the goal is a cryptographic key.
  • Node.js: crypto.randomBytes(), which "generates cryptographically strong pseudorandom data" (Node.js).
  • Python: the secrets module, which "should be used in preference to the default pseudo-random number generator in the random module, which is designed for modelling and simulation, not security or cryptography" (Python).
  • The command line: openssl rand -base64 32 prints 32 random bytes, 256 bits, from OpenSSL's CSPRNG (OpenSSL).

Common mistakes

  • Math.random() or a language's default rand for tokens, reset codes or keys.
  • Biased conversion. Turning random bytes into characters with byte % alphabetSize makes some characters more likely unless the alphabet size divides 256. The fix is to discard bytes from the uneven tail and draw again.
  • Too few bytes. A random 6-character string isn't a strong secret, however good the generator. See entropy.
  • Seeding by hand. Seeding a generator with the time or a process ID throws away the protection.

Where Crunu uses it

The Generate button in Crunu's environment editor creates a 40-character value of letters and digits from crypto.getRandomValues() in your browser. It discards bytes that would bias the result, so every character is equally likely, which gives about 238 bits. The value is created on your device and saved encrypted like any other value; the server never sees it in plain form.

For values you need outside the vault, such as a framework's signing secret, the free secret key generator works the same way in your browser and offers Base64, Base64URL, hex or letters and digits from 128 to 512 bits. More on handling the result: how to share .env files with your team.

Sources

From the blog

  • Secrets

    NEXT_PUBLIC_ leaks: how secrets get into a JavaScript bundle

    Any variable with a public prefix is shipped to every visitor. How NEXT_PUBLIC_, VITE_, PUBLIC_ and NUXT_PUBLIC_ work, how to check your bundle, and what to do.

    Sharing .env files guide

  • Practices

    Rotating API keys without downtime: the overlap window

    Issue the new key, roll it out, confirm the old one is idle, then revoke it. How AWS, Stripe and databases support overlap, and what to do when they don't.

    Offboarding guide

  • Security

    Why a vault vendor can’t reset your password

    With a password and a Secret Key, nobody at the vendor can open your account, or reset it. How the Emergency Kit works, and what to do when something is lost.

    Who can decrypt guide