use rand::{RngCore, SeedableRng};
use rand_chacha::ChaCha20Rng;
use sha3::{Digest, Sha3_256};
pub const EXAMPLE_SEED: Seed = Seed::from_bytes([
0xbf, 0x18, 0x11, 0xce, 0x15, 0xee, 0xfd, 0x20, 0x2f, 0xdf, 0x67, 0x6a, 0x6b, 0xba, 0xaf, 0x04,
0xff, 0x71, 0xe0, 0xf8, 0x0b, 0x2a, 0xcf, 0x27, 0x85, 0xb3, 0x32, 0xc6, 0x20, 0x80, 0x5e, 0x36,
]);
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub struct Seed {
pub bytes: [u8; 32],
}
impl Seed {
#[inline]
pub const fn from_bytes(bytes: [u8; 32]) -> Self {
Self { bytes }
}
#[inline]
pub fn get_rng(self) -> ChaCha20Rng {
ChaCha20Rng::from_seed(self.bytes)
}
#[inline]
pub fn next(self) -> Self {
let mut bytes = [0; 32];
self.get_rng().fill_bytes(&mut bytes);
Self::from_bytes(bytes)
}
pub fn fork(&self, key: &str) -> Self {
let mut seed = self.next();
let forked_seed = &mut seed.bytes;
let hash = Sha3_256::digest(key.as_bytes());
for i in 0..32 {
forked_seed[i] ^= hash[i];
}
seed.next()
}
}