use rand::RngCore;
pub fn secure_random_bytes(buf: &mut [u8]) {
rand::thread_rng().fill_bytes(buf);
}
pub fn random_bytes(len: usize) -> Vec<u8> {
let mut buf = vec![0u8; len];
secure_random_bytes(&mut buf);
buf
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fills_buffer() {
let mut buf = [0u8; 64];
secure_random_bytes(&mut buf);
assert_ne!(buf, [0u8; 64]);
}
#[test]
fn random_bytes_length() {
let bytes = random_bytes(42);
assert_eq!(bytes.len(), 42);
}
#[test]
fn two_calls_differ() {
let mut a = [0u8; 32];
let mut b = [0u8; 32];
secure_random_bytes(&mut a);
secure_random_bytes(&mut b);
assert_ne!(a, b);
}
}