Struct ReseedingRng

struct ReseedingRng<R, Rsdr>(_)
where
    R: BlockRngCore + SeedableRng,
    Rsdr: TryRngCore

A wrapper around any PRNG that implements BlockRngCore, that adds the ability to reseed it.

ReseedingRng reseeds the underlying PRNG in the following cases:

When should reseeding after a fixed number of generated bytes be used?

Reseeding after a fixed number of generated bytes is never strictly necessary. Cryptographic PRNGs don't have a limited number of bytes they can output, or at least not a limit reachable in any practical way. There is no such thing as 'running out of entropy'.

Occasionally reseeding can be seen as some form of 'security in depth'. Even if in the future a cryptographic weakness is found in the CSPRNG being used, or a flaw in the implementation, occasionally reseeding should make exploiting it much more difficult or even impossible.

Use ReseedingRng::new with a threshold of 0 to disable reseeding after a fixed number of generated bytes.

Error handling

Although unlikely, reseeding the wrapped PRNG can fail. ReseedingRng will never panic but try to handle the error intelligently through some combination of retrying and delaying reseeding until later. If handling the source error fails ReseedingRng will continue generating data from the wrapped PRNG without reseeding.

Manually calling reseed() will not have this retry or delay logic, but reports the error.

Example

use rand::prelude::*;
use rand_chacha::ChaCha20Core; // Internal part of ChaChaRng that
                             // implements BlockRngCore
use rand::rngs::OsRng;
use rand::rngs::ReseedingRng;

let mut reseeding_rng = ReseedingRng::<ChaCha20Core, _>::new(0, OsRng).unwrap();

println!("{}", reseeding_rng.random::<u64>());

let mut cloned_rng = reseeding_rng.clone();
assert!(reseeding_rng.random::<u64>() != cloned_rng.random::<u64>());

Implementations

impl<R, Rsdr> ReseedingRng<R, Rsdr>

fn new(threshold: u64, reseeder: Rsdr) -> Result<Self, <Rsdr as >::Error>

Create a new ReseedingRng from an existing PRNG, combined with a RNG to use as reseeder.

threshold sets the number of generated bytes after which to reseed the PRNG. Set it to zero to never reseed based on the number of generated values.

fn reseed(self: &mut Self) -> Result<(), <Rsdr as >::Error>

Immediately reseed the generator

This discards any remaining random data in the cache.

impl<R> Rng for ReseedingRng<R, Rsdr>

impl<R> TryCryptoRng for ReseedingRng<R, Rsdr>

impl<R> TryRngCore for ReseedingRng<R, Rsdr>

fn try_next_u32(self: &mut Self) -> Result<u32, <R as TryRngCore>::Error>
fn try_next_u64(self: &mut Self) -> Result<u64, <R as TryRngCore>::Error>
fn try_fill_bytes(self: &mut Self, dst: &mut [u8]) -> Result<(), <R as TryRngCore>::Error>

impl<R, Rsdr> Clone for ReseedingRng<R, Rsdr>

fn clone(self: &Self) -> ReseedingRng<R, Rsdr>

impl<R, Rsdr> CryptoRng for ReseedingRng<R, Rsdr>

impl<R, Rsdr> Debug for ReseedingRng<R, Rsdr>

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

impl<R, Rsdr> Freeze for ReseedingRng<R, Rsdr>

impl<R, Rsdr> RefUnwindSafe for ReseedingRng<R, Rsdr>

impl<R, Rsdr> RngCore for ReseedingRng<R, Rsdr>

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

impl<R, Rsdr> Send for ReseedingRng<R, Rsdr>

impl<R, Rsdr> Sync for ReseedingRng<R, Rsdr>

impl<R, Rsdr> Unpin for ReseedingRng<R, Rsdr>

impl<R, Rsdr> UnsafeUnpin for ReseedingRng<R, Rsdr>

impl<R, Rsdr> UnwindSafe for ReseedingRng<R, Rsdr>

impl<T> Any for ReseedingRng<R, Rsdr>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for ReseedingRng<R, Rsdr>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for ReseedingRng<R, Rsdr>

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

impl<T> CloneToUninit for ReseedingRng<R, Rsdr>

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for ReseedingRng<R, Rsdr>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for ReseedingRng<R, Rsdr>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for ReseedingRng<R, Rsdr>

fn into(self: 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 for ReseedingRng<R, Rsdr>

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

impl<T, U> TryInto for ReseedingRng<R, Rsdr>

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

impl<V, T> VZip for ReseedingRng<R, Rsdr>

fn vzip(self: Self) -> V