1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//! Random Number generation for the Mesh.
//! Generalized over the rand Library so there's no hard dependencies.

use rand::distributions::{Distribution, Standard};
use rand::RngCore;

pub trait Randomizable: Sized {
    /// Generates and returns a random `T`. Currently essentially just an alias for `rand::random`
    /// Assume `random` to be not secure! Even though `random` could use a cryptographically secure
    /// random number generator behind the scenes, use `random_secure` if you need crypto-random.
    fn random() -> Self {
        Self::random_secure()
    }
    /// Generates and returns a cryptographically secure random `T`.
    fn random_secure() -> Self;
}
pub fn secure_random_fill_bytes(bytes: &mut [u8]) {
    rand::thread_rng().fill_bytes(bytes)
}
impl<T> Randomizable for T
where
    Standard: Distribution<T>,
{
    fn random_secure() -> Self {
        rand::random()
    }
}