Skip to main content

crypto/
random.rs

1use crate::RandomError;
2
3/// Returns an array filled with cryptographically-random data.
4///
5/// Returns [`RandomError`] when the operating system's random number
6/// generator is unavailable or fails.
7#[inline]
8pub fn bytes<const N: usize>() -> Result<[u8; N], RandomError> {
9    let mut buf = [0u8; N];
10    getrandom::fill(&mut buf)?;
11    Ok(buf)
12}
13
14/// Fills the given buffer with cryptographically-random data.
15///
16/// Returns [`RandomError`] when the operating system's random number
17/// generator is unavailable or fails.
18#[inline]
19pub fn fill(buf: &mut [u8]) -> Result<(), RandomError> {
20    getrandom::fill(buf)?;
21    Ok(())
22}