rustls/rustls/src/rand.rs

31 lines
699 B
Rust
Raw Normal View History

/// The single place where we generate random material
/// for our own use. These functions never fail,
/// they panic on error.
use ring::rand::{SystemRandom, SecureRandom};
2019-01-20 17:24:27 +00:00
use crate::msgs::codec;
/// Fill the whole slice with random material.
pub fn fill_random(bytes: &mut [u8]) {
SystemRandom::new()
.fill(bytes)
.unwrap();
}
2017-12-02 16:30:56 +00:00
/// Make a Vec<u8> of the given size
/// containing random material.
pub fn random_vec(len: usize) -> Vec<u8> {
2019-05-07 19:08:19 +00:00
let mut v = vec![0; len];
2017-12-02 16:30:56 +00:00
fill_random(&mut v);
v
}
2017-02-15 00:20:28 +00:00
/// Return a uniformly random u32.
pub fn random_u32() -> u32 {
let mut buf = [0u8; 4];
fill_random(&mut buf);
codec::decode_u32(&buf)
.unwrap()
}