HKDFRng

Struct HKDFRng 

Source
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

Source

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 from
  • salt - A salt value to mix with the key material
  • page_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();
Source

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 from
  • salt - 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 Drop for HKDFRng

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

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.

Source§

fn next_u32(&mut self) -> u32

Generates a random u32 value.

§Returns

A deterministic random 32-bit unsigned integer.

Source§

fn next_u64(&mut self) -> u64

Generates a random u64 value.

§Returns

A deterministic random 64-bit unsigned integer.

Source§

fn fill_bytes(&mut self, dest: &mut [u8])

Fills the provided buffer with random bytes.

§Parameters
  • dest - The buffer to fill with random bytes
Source§

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

Attempts to fill the provided buffer with random bytes.

This implementation never fails, so it simply calls fill_bytes.

§Parameters
  • dest - The buffer to fill with random bytes
§Returns

Always returns Ok(()) as this implementation cannot fail.

Source§

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CryptoRngCore for T
where T: CryptoRng + RngCore,

Source§

fn as_rngcore(&mut self) -> &mut dyn RngCore

Upcast to an RngCore trait object.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<R> RandBigInt for R
where R: Rng + ?Sized,

Source§

fn gen_biguint(&mut self, bit_size: usize) -> BigUint

Generate a random BigUint of the given bit size.
Source§

fn gen_bigint(&mut self, bit_size: usize) -> BigInt

Generate a random BigInt of the given bit size.
Source§

fn gen_biguint_below(&mut self, bound: &BigUint) -> BigUint

Generate a random BigUint less than the given bound. Fails when the bound is zero.
Source§

fn gen_biguint_range(&mut self, lbound: &BigUint, ubound: &BigUint) -> BigUint

Generate a random BigUint within the given range. The lower bound is inclusive; the upper bound is exclusive. Fails when the upper bound is not greater than the lower bound.
Source§

fn gen_bigint_range(&mut self, lbound: &BigInt, ubound: &BigInt) -> BigInt

Generate a random BigInt within the given range. The lower bound is inclusive; the upper bound is exclusive. Fails when the upper bound is not greater than the lower bound.
Source§

impl<R> RandPrime for R
where R: Rng + ?Sized,

Source§

fn gen_prime(&mut self, bit_size: usize) -> BigUint

Generate a random prime number with as many bits as given.
Source§

impl<R> Rng for R
where R: RngCore + ?Sized,

Source§

fn gen<T>(&mut self) -> T

Return a random value supporting the Standard distribution. Read more
Source§

fn gen_range<T, R>(&mut self, range: R) -> T
where T: SampleUniform, R: SampleRange<T>,

Generate a random value in the given range. Read more
Source§

fn sample<T, D>(&mut self, distr: D) -> T
where D: Distribution<T>,

Sample a new value, using the given distribution. Read more
Source§

fn sample_iter<T, D>(self, distr: D) -> DistIter<D, Self, T>
where D: Distribution<T>, Self: Sized,

Create an iterator that generates values using the given distribution. Read more
Source§

fn fill<T>(&mut self, dest: &mut T)
where T: Fill + ?Sized,

Fill any type implementing Fill with random data Read more
Source§

fn try_fill<T>(&mut self, dest: &mut T) -> Result<(), Error>
where T: Fill + ?Sized,

Fill any type implementing Fill with random data Read more
Source§

fn gen_bool(&mut self, p: f64) -> bool

Return a bool with a probability p of being true. Read more
Source§

fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool

Return a bool with a probability of 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,