pub trait EntropySource {
fn fill(&mut self, dst: &mut [u8]);
}
#[derive(Debug, Clone)]
pub struct TestEntropySource {
counter: u64,
}
impl TestEntropySource {
#[must_use]
pub fn noxtls_new() -> Self {
Self { counter: 0 }
}
}
impl EntropySource for TestEntropySource {
fn fill(&mut self, dst: &mut [u8]) {
for byte in dst.iter_mut() {
*byte = (self.counter & 0xff) as u8;
self.counter = self.counter.wrapping_add(1);
}
}
}
#[cfg(feature = "rand-core")]
pub struct RandCoreEntropy<R>(pub R);
#[cfg(feature = "rand-core")]
impl<R: rand_core::RngCore> EntropySource for RandCoreEntropy<R> {
fn fill(&mut self, dst: &mut [u8]) {
self.0.fill_bytes(dst);
}
}
#[cfg(feature = "std")]
#[derive(Debug, Clone, Copy, Default)]
pub struct StdEntropy;
#[cfg(feature = "std")]
impl EntropySource for StdEntropy {
fn fill(&mut self, dst: &mut [u8]) {
getrandom::fill(dst).expect("OS entropy source failed");
}
}