1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//! Utilities that provide randomness.
//! Note that this currently uses the rand crate and should be moved to a more
//! secure alternative.
//!

use rand::{self, thread_rng, Fill, RngCore};
use rand_core::OsRng;

/// Generate a random byte vector of length `len`.
/// *PANICS* if randomness generation fails.
pub fn random_vec(len: usize) -> Vec<u8> {
    let mut out = vec![0u8; len];
    out.try_fill(&mut thread_rng()).unwrap();
    out
}

/// Generate a random array.
/// *PANICS* if randomness generation fails.
pub fn random_array<const N: usize>() -> [u8; N] {
    let mut out = [0u8; N];
    OsRng.fill_bytes(&mut out);
    out
}