use std::io;
#[derive(Debug)]
pub enum EntropyError {
Io(io::Error),
ShortRead { actual: usize },
}
pub fn random_bytes_32() -> Result<[u8; 32], EntropyError> {
#[cfg(unix)]
{
use std::{fs::File, io::Read};
let mut source = File::open("/dev/urandom").map_err(EntropyError::Io)?;
let mut bytes = [0; 32];
let mut filled = 0;
while filled < bytes.len() {
let current = match source.read(&mut bytes[filled..]) {
Ok(current) => current,
Err(source) if source.kind() == io::ErrorKind::Interrupted => continue,
Err(source) => return Err(EntropyError::Io(source)),
};
if current == 0 {
return Err(EntropyError::ShortRead { actual: filled });
}
filled += current;
}
Ok(bytes)
}
#[cfg(not(unix))]
{
Err(EntropyError::Io(io::Error::new(
io::ErrorKind::Unsupported,
"cryptographic identity generation requires a Unix entropy source",
)))
}
}