Use cryptographically 安全 pseudorandom 数字 generator
CSPRNGs 生成输出 indistinguishable 从真 randomness. Learn 正确 APIs 和为什么 Math.random() 是禁止在 安全 contexts.
- 1
CSPRNG collects entropy 从 hardware 源 (thermal 噪声, keystroke timing, 磁盘 seek times) 和 produces 输出该 是 computationally indistinguishable 从真 randomness. 在 browsers, use crypto.getRandomValues(); 在 Node.js, use crypto.randomBytes(). 从不 use Math.random() — 它是 PRNG 为 simulations, 不安全.
- 2 随机字节生成器 →
生成 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 密码生成器 →
生成 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
Entropy 检查: CSPRNG 是仅 as 好 as 其 entropy 源. 开 startup, OS entropy pool 可以是 低 — Node.js 和 browsers 处理此 通过 blocking 直到 enough entropy 是 collected. 开 embedded devices 或 VMs, 考虑 adding hardware entropy 源 (e.g., RDRAND, /dev/hwrng).
常见问题
- 为什么是 Math.random() 不安全?
- Math.random() 我们 deterministic algorithm (e.g., xorshift) seeded 从 predictable 值. 具有 enough 输出, attacker 能够 reconstruct 内部声明和 predict 所有 future 输出. 它是 designed 为速度在 simulations, 不 unpredictability 为安全.
- 什么是 entropy?
- Entropy 是测量的 unpredictability. CSPRNG needs high-entropy 输入 (从 hardware 噪声) 到生成安全输出. 开 fresh VM, entropy pool 可以是 小 — use haveged 或 virtio-rng 到 supplement 它. 低 entropy leads 到 predictable "random" 输出.