pub struct SecureRandom { /* private fields */ }Expand description
Cryptographically secure random number generator
This generator uses multiple entropy sources and a cryptographically secure PRNG to provide high-quality random numbers suitable for security applications.
Implementations§
Source§impl SecureRandom
impl SecureRandom
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new cryptographically secure RNG
Uses multiple entropy sources including:
- System time (nanoseconds since UNIX epoch)
- Process ID
- Thread ID
- Hash combining all sources
Sourcepub fn from_seed(seed: [u8; 32]) -> Self
pub fn from_seed(seed: [u8; 32]) -> Self
Create a secure RNG from a provided seed
§Warning
Using a predictable seed defeats the purpose of cryptographic security. This method should only be used for testing or when the seed comes from a high-entropy source.
Sourcepub fn sample<D, T>(&mut self, distribution: D) -> Twhere
D: Distribution<T>,
pub fn sample<D, T>(&mut self, distribution: D) -> Twhere
D: Distribution<T>,
Generate a cryptographically secure random value
Sourcepub fn random_bytes(&mut self, count: usize) -> Vec<u8> ⓘ
pub fn random_bytes(&mut self, count: usize) -> Vec<u8> ⓘ
Generate cryptographically secure random bytes
This is suitable for generating keys, nonces, and other cryptographic material.
Sourcepub fn generate_key(&mut self, length: usize) -> Vec<u8> ⓘ
pub fn generate_key(&mut self, length: usize) -> Vec<u8> ⓘ
Generate a cryptographically secure random key
Alias for random_bytes with more descriptive naming for key generation.
Sourcepub fn generate_nonce(&mut self, length: usize) -> Vec<u8> ⓘ
pub fn generate_nonce(&mut self, length: usize) -> Vec<u8> ⓘ
Generate a cryptographically secure random nonce
Nonces should be unique for each use. This generates random bytes suitable for use as a nonce in cryptographic protocols.
Sourcepub fn random_f64(&mut self) -> f64
pub fn random_f64(&mut self) -> f64
Generate a cryptographically secure random float in [0, 1)
Sourcepub fn random_f32(&mut self) -> f32
pub fn random_f32(&mut self) -> f32
Generate a cryptographically secure random float in [0, 1) as f32
Sourcepub fn random_range<T>(&mut self, min: T, max: T) -> T
pub fn random_range<T>(&mut self, min: T, max: T) -> T
Generate cryptographically secure random integers in a range
Sourcepub fn random_bool(&mut self) -> bool
pub fn random_bool(&mut self) -> bool
Generate a cryptographically secure random boolean
Sourcepub fn random_bool_with_probability(&mut self, p: f64) -> bool
pub fn random_bool_with_probability(&mut self, p: f64) -> bool
Generate a cryptographically secure random boolean with given probability
Sourcepub fn random_alphanumeric(&mut self, length: usize) -> String
pub fn random_alphanumeric(&mut self, length: usize) -> String
Generate cryptographically secure random alphanumeric string
Useful for generating random passwords, tokens, or identifiers.
Sourcepub fn random_hex(&mut self, byte_length: usize) -> String
pub fn random_hex(&mut self, byte_length: usize) -> String
Generate cryptographically secure random hex string
Useful for generating random hex-encoded keys or identifiers.
Sourcepub fn random_uuid(&mut self) -> String
pub fn random_uuid(&mut self) -> String
Generate cryptographically secure UUID v4
Generates a random UUID suitable for use as a unique identifier.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SecureRandom
impl RefUnwindSafe for SecureRandom
impl Send for SecureRandom
impl Sync for SecureRandom
impl Unpin for SecureRandom
impl UnwindSafe for SecureRandom
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
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.