Skip to content

Secure random number generation.#35

Merged
dipu-bd merged 9 commits intobitanon:masterfrom
elliotwutingfeng:jsrandom
Dec 20, 2025
Merged

Secure random number generation.#35
dipu-bd merged 9 commits intobitanon:masterfrom
elliotwutingfeng:jsrandom

Conversation

@elliotwutingfeng
Copy link
Contributor

@elliotwutingfeng elliotwutingfeng commented Dec 16, 2025

Summary

  • Add a Node-compatible secure Random implementation that uses Node's crypto.randomInt(); all other JS platforms are compatible with the default Random.secure().
  • Avoid mixing predictable datetime output with CSPRNG output.

Tested with

dart test -p vm,chrome,node -c kernel,dart2js,dart2wasm test/random/random_test.dart

Changes

  • generator_vm.dart
    • Avoid mixing predictable datetime output with CSPRNG output.
  • generator_js.dart
    • Avoid mixing predictable datetime output with CSPRNG output.
    • Added NodeRandom as an implementation of Random for NodeJS.
    • nextInt(max) draws 32-bit values from Node crypto, supports max ∈ [1, 2^32].
    • secureRandom() uses Random.secure() for web browsers, and NodeRandom for Node.js.
    • $generateSeed() delegates to secureRandom().
  • generators.dart
    • Fixed spelling of _hashGenerator

Notes

  • Consider adjusting $generateSeed() to request the full 2^32 range (_mask32 + 1) to cover the entire 32-bit space.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants