noxtls_platform/
entropy.rs1pub trait EntropySource {
10 fn fill(&mut self, dst: &mut [u8]);
12}
13
14#[derive(Debug, Clone)]
16pub struct TestEntropySource {
17 counter: u64,
18}
19
20impl TestEntropySource {
21 #[must_use]
23 pub fn noxtls_new() -> Self {
24 Self { counter: 0 }
25 }
26}
27
28impl EntropySource for TestEntropySource {
29 fn fill(&mut self, dst: &mut [u8]) {
30 for byte in dst.iter_mut() {
31 *byte = (self.counter & 0xff) as u8;
32 self.counter = self.counter.wrapping_add(1);
33 }
34 }
35}
36
37#[cfg(feature = "rand-core")]
39pub struct RandCoreEntropy<R>(pub R);
40
41#[cfg(feature = "rand-core")]
42impl<R: rand_core::RngCore> EntropySource for RandCoreEntropy<R> {
43 fn fill(&mut self, dst: &mut [u8]) {
44 self.0.fill_bytes(dst);
45 }
46}
47
48#[cfg(feature = "std")]
50#[derive(Debug, Clone, Copy, Default)]
51pub struct StdEntropy;
52
53#[cfg(feature = "std")]
54impl EntropySource for StdEntropy {
55 fn fill(&mut self, dst: &mut [u8]) {
56 getrandom::fill(dst).expect("OS entropy source failed");
57 }
58}