veilid/veilid-tools/src/random.rs

41 lines
788 B
Rust
Raw Normal View History

2022-11-27 02:37:23 +00:00
use rand::prelude::*;
#[derive(Clone, Copy, Debug, Default)]
pub struct VeilidRng;
impl CryptoRng for VeilidRng {}
impl RngCore for VeilidRng {
fn next_u32(&mut self) -> u32 {
get_random_u32()
}
fn next_u64(&mut self) -> u64 {
get_random_u64()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
2023-05-29 19:24:57 +00:00
random_bytes(dest);
2022-11-27 02:37:23 +00:00
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
2023-05-29 19:24:57 +00:00
random_bytes(dest);
Ok(())
2022-11-27 02:37:23 +00:00
}
}
pub fn random_bytes(dest: &mut [u8]) {
let mut rng = rand::thread_rng();
rng.fill_bytes(dest);
}
2022-11-27 02:37:23 +00:00
pub fn get_random_u32() -> u32 {
let mut rng = rand::thread_rng();
rng.next_u32()
}
2022-11-27 02:37:23 +00:00
pub fn get_random_u64() -> u64 {
let mut rng = rand::thread_rng();
rng.next_u64()
2022-11-27 02:37:23 +00:00
}