pub struct ReseedingRng<R, Rsdr>(/* private fields */)
where
R: BlockRngCore + SeedableRng,
Rsdr: TryRngCore;Expand description
A wrapper around any PRNG that implements BlockRngCore, that adds the
ability to reseed it.
ReseedingRng reseeds the underlying PRNG in the following cases:
- On a manual call to
reseed(). - After
clone(), the clone will be reseeded on first use. - After the PRNG has generated a configurable number of random bytes.
§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§
Source§impl<R, Rsdr> ReseedingRng<R, Rsdr>
impl<R, Rsdr> ReseedingRng<R, Rsdr>
Sourcepub fn new(threshold: u64, reseeder: Rsdr) -> Result<Self, Rsdr::Error>
pub fn new(threshold: u64, reseeder: Rsdr) -> Result<Self, Rsdr::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.
Trait Implementations§
Source§impl<R, Rsdr> Clone for ReseedingRng<R, Rsdr>
impl<R, Rsdr> Clone for ReseedingRng<R, Rsdr>
Source§fn clone(&self) -> ReseedingRng<R, Rsdr>
fn clone(&self) -> ReseedingRng<R, Rsdr>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl<R, Rsdr> CryptoRng for ReseedingRng<R, Rsdr>
Source§impl<R, Rsdr> Debug for ReseedingRng<R, Rsdr>
impl<R, Rsdr> Debug for ReseedingRng<R, Rsdr>
Source§impl<R, Rsdr> RngCore for ReseedingRng<R, Rsdr>
impl<R, Rsdr> RngCore for ReseedingRng<R, Rsdr>
Auto Trait Implementations§
impl<R, Rsdr> Freeze for ReseedingRng<R, Rsdr>
impl<R, Rsdr> RefUnwindSafe for ReseedingRng<R, Rsdr>
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>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T, I> IntoFallibleInit<T> for Iwhere
I: Init<T>,
impl<T, I> IntoFallibleInit<T> for Iwhere
I: Init<T>,
Source§fn into_fallible<E>(self) -> impl Init<T, E>
fn into_fallible<E>(self) -> impl Init<T, E>
Source§impl<Source, Target> OctetsInto<Target> for Sourcewhere
Target: OctetsFrom<Source>,
impl<Source, Target> OctetsInto<Target> for Sourcewhere
Target: OctetsFrom<Source>,
type Error = <Target as OctetsFrom<Source>>::Error
Source§fn try_octets_into(
self,
) -> Result<Target, <Source as OctetsInto<Target>>::Error>
fn try_octets_into( self, ) -> Result<Target, <Source as OctetsInto<Target>>::Error>
Source§fn octets_into(self) -> Target
fn octets_into(self) -> Target
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
impl<R> TryCryptoRng for R
Source§impl<R> TryRngCore for R
impl<R> TryRngCore for R
Source§type Error = Infallible
type Error = Infallible
Source§fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
u32.Source§fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
u64.Source§fn try_fill_bytes(
&mut self,
dst: &mut [u8],
) -> Result<(), <R as TryRngCore>::Error>
fn try_fill_bytes( &mut self, dst: &mut [u8], ) -> Result<(), <R as TryRngCore>::Error>
dest entirely with random data.Source§fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>
fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>
UnwrapMut wrapper.