Struct ChaCha8Rng

pub struct ChaCha8Rng { /* private fields */ }

A cryptographically secure random number generator that uses the ChaCha stream cipher.

See the [crate docs][crate] for more information about the underlying stream cipher.

This RNG implementation uses a 64-bit counter and 64-bit stream identifier (a.k.a nonce). A 64-bit counter over 64-byte (16 word) blocks allows 1 ZiB of output before cycling, and the stream identifier allows 264 unique streams of output per seed. Both counter and stream are initialized to zero but may be set via the set_word_pos and set_stream methods.

Example

use chacha20::ChaCha8Rng;
use rand_core::{SeedableRng, Rng};

let seed = [42u8; 32];
let mut rng = ChaCha8Rng::from_seed(seed);

let random_u32 = rng.next_u32();
let random_u64 = rng.next_u64();

let mut random_bytes = [0u8; 3];
rng.fill_bytes(&mut random_bytes);

See the rand crate for more advanced RNG functionality.

Implementations

impl ChaCha8Rng

fn get_word_pos(&self) -> u128

Get the offset from the start of the stream, in 32-bit words.

Since the generated blocks are 16 words (24) long and the counter is 64-bits, the offset is a 68-bit number. Sub-word offsets are not supported, hence the result can simply be multiplied by 4 to get a byte-offset.

fn set_word_pos(&mut self, word_offset: u128)

Set the offset from the start of the stream, in 32-bit words.

This value will be erased when calling set_stream(), so call set_stream() before calling set_word_pos() if you intend on using both of them together.

As with get_word_pos, we use a 68-bit number. Since the generator simply cycles at the end of its period (1 ZiB), we ignore the upper 60 bits.

fn set_block_pos(&mut self, block_pos: u64)

Sets the block pos and resets the RNG's index.

This value will be erased when calling set_stream(), so call set_stream() before calling set_block_pos() if you intend on using both of them together.

The word pos will be equal to block_pos * 16 words per block.

fn get_block_pos(&self) -> u64

Get the block pos.

fn set_stream(&mut self, stream: u64)

Set the stream ID and reset the word_pos to 0.

fn get_stream(&self) -> u64

Get the stream number (nonce).

fn get_seed(&self) -> [u8; 32]

Get the RNG seed.

fn serialize_state(&self) -> SerializedRngState

Serialize RNG state.

Warning

Leaking serialized RNG state to an attacker defeats security properties provided by the RNG.

fn deserialize_state(state: &SerializedRngState) -> Self

Deserialize RNG state.

Trait Implementations

impl Debug for ChaCha8Rng

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl Eq for ChaCha8Rng

impl PartialEq for ChaCha8Rng

fn eq(&self, rhs: &ChaCha8Rng) -> bool

impl SeedableRng for ChaCha8Rng

type Seed = [u8; 32];
fn from_seed(seed: Self::Seed) -> Self

impl TryCryptoRng for ChaCha8Rng

impl TryRng for ChaCha8Rng

type Error = Infallible;
fn try_next_u32(&mut self) -> Result<u32, Self::Error>
fn try_next_u64(&mut self) -> Result<u64, Self::Error>
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error>

Auto Trait Implementations

impl Freeze for ChaCha8Rng

impl RefUnwindSafe for ChaCha8Rng

impl Send for ChaCha8Rng

impl Sync for ChaCha8Rng

impl Unpin for ChaCha8Rng

impl UnsafeUnpin for ChaCha8Rng

impl UnwindSafe for ChaCha8Rng

Blanket Implementations

impl<R> CryptoRng for ChaCha8Rng where R: TryCryptoRng<Error = Infallible> + ?Sized,

impl<R> Rng for ChaCha8Rng where R: TryRng<Error = Infallible> + ?Sized,

fn next_u32(&mut self) -> u32
fn next_u64(&mut self) -> u64
fn fill_bytes(&mut self, dst: &mut [u8])

impl<R> RngCore for ChaCha8Rng where R: Rng,

impl<R> TryRngCore for ChaCha8Rng where R: TryRng,

type Error = <R as TryRng>::Error;

impl<T> Any for ChaCha8Rng where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for ChaCha8Rng where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for ChaCha8Rng where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for ChaCha8Rng

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into<U> for ChaCha8Rng where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for ChaCha8Rng where U: Into<T>,

type Error = Infallible;
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto<U> for ChaCha8Rng where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>