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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#![allow(deprecated)]
use crate::rngs::OsRng;
use rand_core::{CryptoRng, Error, RngCore};
#[derive(Debug)]
#[deprecated(since = "0.7.0", note = "use rngs::OsRng instead")]
pub struct EntropyRng {
source: OsRng,
}
impl EntropyRng {
pub fn new() -> Self {
EntropyRng { source: OsRng }
}
}
impl Default for EntropyRng {
fn default() -> Self {
EntropyRng::new()
}
}
impl RngCore for EntropyRng {
fn next_u32(&mut self) -> u32 {
self.source.next_u32()
}
fn next_u64(&mut self) -> u64 {
self.source.next_u64()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.source.fill_bytes(dest)
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.source.try_fill_bytes(dest)
}
}
impl CryptoRng for EntropyRng {}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_entropy() {
let mut rng = EntropyRng::new();
let n = (rng.next_u32() ^ rng.next_u32()).count_ones();
assert!(n >= 2);
}
}