Skip to main content

SecureRng

Trait SecureRng 

Source
pub trait SecureRng:
    Rng
    + CryptoRng
    + Send
    + Sync {
    // Provided methods
    fn fill_bytes_secure(&mut self, dest: &mut [u8]) -> Result<()> { ... }
    fn next_u32_secure(&mut self) -> Result<u32> { ... }
    fn next_u64_secure(&mut self) -> Result<u64> { ... }
    fn initialize(&mut self, entropy: &[u8]) -> Result<()> { ... }
    fn is_secure(&self) -> bool { ... }
    fn entropy_quality(&self) -> f64 { ... }
    fn security_level(&self) -> SecurityLevel { ... }
    fn reseed(&mut self) -> Result<()> { ... }
    fn state_size(&self) -> usize { ... }
    fn reseed_interval(&self) -> Option<usize> { ... }
}
Expand description

Enhanced random number generator trait for cryptographic applications

This trait extends the standard Rng and CryptoRng traits with additional functionality required for secure cryptographic operations.

Provided Methods§

Source

fn fill_bytes_secure(&mut self, dest: &mut [u8]) -> Result<()>

Fill the buffer with cryptographically secure random bytes

This method provides the same functionality as Rng::fill_bytes but with additional security guarantees and error handling.

§Arguments
  • dest - Buffer to fill with random bytes
§Errors

Returns an error if the random number generation fails or if the generated bytes don’t meet security requirements.

Source

fn next_u32_secure(&mut self) -> Result<u32>

Generate a random u32 with security validation

§Errors

Returns an error if the random number generation fails.

Source

fn next_u64_secure(&mut self) -> Result<u64>

Generate a random u64 with security validation

§Errors

Returns an error if the random number generation fails.

Source

fn initialize(&mut self, entropy: &[u8]) -> Result<()>

Initialize the RNG with entropy (if supported)

§Arguments
  • entropy - Entropy data to initialize the RNG
§Errors

Returns an error if the entropy is invalid or initialization fails.

Source

fn is_secure(&self) -> bool

Check if the RNG is cryptographically secure

Returns true if the RNG provides cryptographically secure randomness, false otherwise (e.g., for deterministic RNGs used in testing).

Source

fn entropy_quality(&self) -> f64

Get the entropy quality estimate (0.0 to 1.0)

Returns an estimate of the entropy quality, where 1.0 represents perfect entropy and 0.0 represents no entropy.

Source

fn security_level(&self) -> SecurityLevel

Get the RNG’s security level

Returns a description of the RNG’s security characteristics.

Source

fn reseed(&mut self) -> Result<()>

Reseed the RNG with fresh entropy

This method allows the RNG to be reseeded with fresh entropy, which is important for long-running applications.

§Errors

Returns an error if reseeding fails or is not supported.

Source

fn state_size(&self) -> usize

Get the RNG’s internal state size

Returns the size of the RNG’s internal state in bytes.

Source

fn reseed_interval(&self) -> Option<usize>

Get the RNG’s reseed interval

Returns the recommended number of bytes to generate before reseeding, or None if reseeding is not required.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl SecureRng for LibQRng

Available on crate feature alloc only.