pub struct HKDFRng { /* private fields */ }Expand description
A deterministic random number generator based on HKDF-HMAC-SHA256.
HKDFRng uses the HMAC-based Key Derivation Function (HKDF) to generate
deterministic random numbers from a combination of key material and salt. It
serves as a key-stretching mechanism that can produce an arbitrary amount of
random-looking bytes from a single seed.
Since it produces deterministic output based on the same inputs, it’s useful for situations where repeatable randomness is required, such as in testing or when deterministically deriving keys from a master seed.
Security considerations:
- The security of the generator depends on the entropy and secrecy of the key material
- The same key material and salt will always produce the same sequence
- Use a secure random seed for cryptographic applications
- Never reuse the same HKDFRng instance for different purposes
The implementation automatically handles buffer management, fetching new data using HKDF as needed with an incrementing counter to ensure unique output for each request.
Implementations§
Source§impl HKDFRng
impl HKDFRng
Sourcepub fn new_with_page_length(
key_material: impl AsRef<[u8]>,
salt: &str,
page_length: usize,
) -> Self
pub fn new_with_page_length( key_material: impl AsRef<[u8]>, salt: &str, page_length: usize, ) -> Self
Creates a new HKDFRng with a custom page length.
§Parameters
key_material- The seed material to derive random numbers fromsalt- A salt value to mix with the key materialpage_length- The number of bytes to generate in each HKDF call
§Returns
A new HKDFRng instance configured with the specified parameters.
§Example
use bc_components::HKDFRng;
use rand_core::RngCore;
// Create an HKDF-based RNG with a 64-byte page length
let mut rng = HKDFRng::new_with_page_length(
b"my secure seed",
"application-context",
64,
);
// Generate some random bytes
let random_u32 = rng.next_u32();Sourcepub fn new(key_material: impl AsRef<[u8]>, salt: &str) -> Self
pub fn new(key_material: impl AsRef<[u8]>, salt: &str) -> Self
Creates a new HKDFRng with the default page length of 32 bytes.
§Parameters
key_material- The seed material to derive random numbers fromsalt- A salt value to mix with the key material
§Returns
A new HKDFRng instance configured with the specified key material and
salt.
§Example
use bc_components::HKDFRng;
use rand_core::RngCore;
// Create an HKDF-based RNG
let mut rng = HKDFRng::new(b"my secure seed", "wallet-derivation");
// Generate two u32 values
let random1 = rng.next_u32();
let random2 = rng.next_u32();
// The same seed and salt will always produce the same sequence
let mut rng2 = HKDFRng::new(b"my secure seed", "wallet-derivation");
assert_eq!(random1, rng2.next_u32());
assert_eq!(random2, rng2.next_u32());Trait Implementations§
Source§impl RngCore for HKDFRng
Implementation of the RngCore trait for HKDFRng.
impl RngCore for HKDFRng
Implementation of the RngCore trait for HKDFRng.
This allows HKDFRng to be used with any code that accepts a random
number generator implementing the standard Rust traits.
impl CryptoRng for HKDFRng
Implementation of the CryptoRng marker trait for HKDFRng.
This marker indicates that HKDFRng is suitable for cryptographic use
when seeded with appropriately secure key material.
Auto Trait Implementations§
impl Freeze for HKDFRng
impl RefUnwindSafe for HKDFRng
impl Send for HKDFRng
impl Sync for HKDFRng
impl Unpin for HKDFRng
impl UnwindSafe for HKDFRng
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> CryptoRngCore for T
impl<T> CryptoRngCore for T
Source§fn as_rngcore(&mut self) -> &mut dyn RngCore
fn as_rngcore(&mut self) -> &mut dyn RngCore
RngCore trait object.Source§impl<R> RandBigInt for R
impl<R> RandBigInt for R
Source§fn gen_biguint(&mut self, bit_size: usize) -> BigUint
fn gen_biguint(&mut self, bit_size: usize) -> BigUint
BigUint of the given bit size.Source§fn gen_bigint(&mut self, bit_size: usize) -> BigInt
fn gen_bigint(&mut self, bit_size: usize) -> BigInt
Source§fn gen_biguint_below(&mut self, bound: &BigUint) -> BigUint
fn gen_biguint_below(&mut self, bound: &BigUint) -> BigUint
BigUint less than the given bound. Fails
when the bound is zero.Source§impl<R> Rng for R
impl<R> Rng for R
Source§fn gen<T>(&mut self) -> Twhere
Standard: Distribution<T>,
fn gen<T>(&mut self) -> Twhere
Standard: Distribution<T>,
Source§fn gen_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
fn gen_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
Source§fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
Source§fn sample_iter<T, D>(self, distr: D) -> DistIter<D, Self, T>where
D: Distribution<T>,
Self: Sized,
fn sample_iter<T, D>(self, distr: D) -> DistIter<D, Self, T>where
D: Distribution<T>,
Self: Sized,
Source§fn gen_bool(&mut self, p: f64) -> bool
fn gen_bool(&mut self, p: f64) -> bool
p of being true. Read moreSource§fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool
fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool
numerator/denominator of being
true. I.e. gen_ratio(2, 3) has chance of 2 in 3, or about 67%, of
returning true. If numerator == denominator, then the returned value
is guaranteed to be true. If numerator == 0, then the returned
value is guaranteed to be false. Read more