Secure Random Numbers in Node.js
Jul 03, 2019 by Lane Wagner
Quick answer: use crypto.randomBytes() for cryptographically secure randomness in Node.js.
const { randomBytes } = await import("node:crypto"); const buf = randomBytes(256); console.log(`${buf.length} bytes of random data: ${buf.toString("hex")}`); crypto.randomBytes() is a cryptographically secure random number generator based on openssl. Depending on the operating system of the user, randomBytes will use /dev/urandom (Unix) or CryptoGenRandom (Windows).