+/// An implementation of [`EntropySource`] using ChaCha20.
+#[derive(Debug)]
+pub struct RandomBytes {
+ /// Seed from which all randomness produced is derived from.
+ seed: [u8; 32],
+ /// Tracks the number of times we've produced randomness to ensure we don't return the same
+ /// bytes twice.
+ index: AtomicCounter,
+}
+
+impl RandomBytes {
+ /// Creates a new instance using the given seed.
+ pub fn new(seed: [u8; 32]) -> Self {
+ Self {
+ seed,
+ index: AtomicCounter::new(),
+ }
+ }
+}
+
+impl EntropySource for RandomBytes {
+ fn get_secure_random_bytes(&self) -> [u8; 32] {
+ let index = self.index.get_increment();
+ let mut nonce = [0u8; 16];
+ nonce[..8].copy_from_slice(&index.to_be_bytes());
+ ChaCha20::get_single_block(&self.seed, &nonce)
+ }
+}
+