Skip to main content

Module rand_chacha

Module rand_chacha 

Source
Expand description

ChaCha-based cryptographically secure random number generators.


rand_chacha provides random number generators based on the ChaCha stream cipher. These are cryptographically secure and portable – given the same seed they produce identical output on all platforms.

Three variants are available, differing in the number of ChaCha rounds:

  • ChaCha20Rng – 20 rounds, the standard choice for cryptographic security.
  • ChaCha12Rng – 12 rounds, a good default balancing security and speed.
  • ChaCha8Rng – 8 rounds, fastest but with a lower security margin.

If you neeed a specific, stable, secure random number generator pick ChaCha12 by default.

ChaCha20 is tha standard strength algorithm and considered secure. ChaCha12 is weaker, but expert consensus is that it is also secure, with no known attacks (it is the the default algorithm of the rand crate’s StdRng, though its algorithm is not specified to be stable).

§Example

use rand::SeedableRng;
use rand::RngExt;
use rand_chacha::ChaCha12Rng;

let mut rng = ChaCha12Rng::seed_from_u64(42);

let value: u32 = rng.random();
let in_range: f64 = rng.random_range(0.0..1.0);
let coin: bool = rng.random();

Modules§

rand_core

Structs§

ChaCha8Core
ChaCha with 8 rounds
ChaCha8Rng
A cryptographically secure random number generator that uses the ChaCha algorithm.
ChaCha12Core
ChaCha with 12 rounds
ChaCha12Rng
A cryptographically secure random number generator that uses the ChaCha algorithm.
ChaCha20Core
ChaCha with 20 rounds
ChaCha20Rng
A cryptographically secure random number generator that uses the ChaCha algorithm.

Type Aliases§

ChaChaCore
ChaCha with 20 rounds, low-level interface
ChaChaRng
ChaCha with 20 rounds