pub struct ChaChaRng { /* private fields */ }
Expand description
A random number generator that uses the ChaCha20 algorithm [1].
The ChaCha algorithm is widely accepted as suitable for
cryptographic purposes, but this implementation has not been
verified as such. Prefer a generator like OsRng
that defers to
the operating system for cases that need high security.
[1]: D. J. Bernstein, ChaCha, a variant of Salsa20
Implementations
sourceimpl ChaChaRng
impl ChaChaRng
sourcepub fn new_unseeded() -> ChaChaRng
pub fn new_unseeded() -> ChaChaRng
Create an ChaCha random number generator using the default fixed key of 8 zero words.
Examples
use sgx_rand::{Rng, ChaChaRng};
let mut ra = ChaChaRng::new_unseeded();
println!("{:?}", ra.next_u32());
println!("{:?}", ra.next_u32());
Since this equivalent to a RNG with a fixed seed, repeated executions of an unseeded RNG will produce the same result. This code sample will consistently produce:
- 2917185654
- 2419978656
sourcepub fn set_counter(&mut self, counter_low: u64, counter_high: u64)
pub fn set_counter(&mut self, counter_low: u64, counter_high: u64)
Sets the internal 128-bit ChaCha counter to a user-provided value. This permits jumping arbitrarily ahead (or backwards) in the pseudorandom stream.
Since the nonce words are used to extend the counter to 128 bits,
users wishing to obtain the conventional ChaCha pseudorandom stream
associated with a particular nonce can call this function with
arguments 0, desired_nonce
.
Examples
use sgx_rand::{Rng, ChaChaRng};
let mut ra = ChaChaRng::new_unseeded();
ra.set_counter(0u64, 1234567890u64);
println!("{:?}", ra.next_u32());
println!("{:?}", ra.next_u32());
Trait Implementations
sourceimpl Rng for ChaChaRng
impl Rng for ChaChaRng
sourcefn next_f32(&mut self) -> f32
fn next_f32(&mut self) -> f32
[0, 1)
. Read moresourcefn next_f64(&mut self) -> f64
fn next_f64(&mut self) -> f64
[0, 1)
. Read moresourcefn fill_bytes(&mut self, dest: &mut [u8])
fn fill_bytes(&mut self, dest: &mut [u8])
dest
with random data. Read moresourcefn gen<T: Rand>(&mut self) -> Twhere
Self: Sized,
fn gen<T: Rand>(&mut self) -> Twhere
Self: Sized,
Rand
type. Read moresourcefn gen_iter<T: Rand>(&mut self) -> Generator<'_, T, Self>ⓘNotable traits for Generator<'a, T, R>impl<'a, T: Rand, R: Rng> Iterator for Generator<'a, T, R> type Item = T;
where
Self: Sized,
fn gen_iter<T: Rand>(&mut self) -> Generator<'_, T, Self>ⓘNotable traits for Generator<'a, T, R>impl<'a, T: Rand, R: Rng> Iterator for Generator<'a, T, R> type Item = T;
where
Self: Sized,
sourcefn gen_range<T: PartialOrd + SampleRange>(&mut self, low: T, high: T) -> Twhere
Self: Sized,
fn gen_range<T: PartialOrd + SampleRange>(&mut self, low: T, high: T) -> Twhere
Self: Sized,
sourcefn gen_weighted_bool(&mut self, n: u32) -> boolwhere
Self: Sized,
fn gen_weighted_bool(&mut self, n: u32) -> boolwhere
Self: Sized,
sourcefn gen_ascii_chars(&mut self) -> AsciiGenerator<'_, Self>ⓘNotable traits for AsciiGenerator<'a, R>impl<'a, R: Rng> Iterator for AsciiGenerator<'a, R> type Item = char;
where
Self: Sized,
fn gen_ascii_chars(&mut self) -> AsciiGenerator<'_, Self>ⓘNotable traits for AsciiGenerator<'a, R>impl<'a, R: Rng> Iterator for AsciiGenerator<'a, R> type Item = char;
where
Self: Sized,
sourcefn choose<'a, T>(&mut self, values: &'a [T]) -> Option<&'a T>where
Self: Sized,
fn choose<'a, T>(&mut self, values: &'a [T]) -> Option<&'a T>where
Self: Sized,
values
. Read more