pub fn fill_random(buffer: &mut [u8]) -> Result<(), String> {
#[cfg(windows)]
{
super::sys::fill_random(buffer)
}
#[cfg(not(windows))]
{
use std::io::Read;
std::fs::File::open("/dev/urandom")
.and_then(|mut file| file.read_exact(buffer))
.map_err(|error| format!("no hay fuente de aleatoriedad: {error}"))
}
}
pub fn random_bytes<const N: usize>() -> Result<[u8; N], String> {
let mut bytes = [0u8; N];
fill_random(&mut bytes)?;
Ok(bytes)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dos_lecturas_difieren_y_no_son_cero() {
let (a, b) = (random_bytes::<32>().unwrap(), random_bytes::<32>().unwrap());
assert_ne!(a, b);
assert_ne!(a, [0; 32]);
}
}