1
//! Prng utilities.
2

            
3
/// Helper to implement rand_core_06 for an Rng providing RngCore from rand_core 0.9.
4
///
5
/// We need this (for now) for compatibility with *-dalek and
6
/// some other crypto libraries.
7
///
8
/// (Please avoid propagating this type outside of the tor-llcrypto crate.
9
/// If you need it for a legacy crypto tool, it is usually better to wrap
10
/// that tool with an API that uses RngCompat.)
11
#[cfg_attr(feature = "rng-compat", visibility::make(pub))]
12
pub struct RngCompat<R>(R);
13

            
14
impl<R> RngCompat<R> {
15
    /// Create a ne RngCompat to wrap `rng`
16
2627
    #[cfg_attr(feature = "rng-compat", visibility::make(pub))]
17
2627
    pub(crate) fn new(rng: R) -> Self {
18
2627
        Self(rng)
19
2627
    }
20
}
21

            
22
impl<R: rand_core::RngCore> rand_core_06::RngCore for RngCompat<R> {
23
    fn next_u32(&mut self) -> u32 {
24
        self.0.next_u32()
25
    }
26

            
27
    fn next_u64(&mut self) -> u64 {
28
        self.0.next_u64()
29
    }
30

            
31
2627
    fn fill_bytes(&mut self, dest: &mut [u8]) {
32
2627
        self.0.fill_bytes(dest);
33
2627
    }
34

            
35
    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core_06::Error> {
36
        self.0.fill_bytes(dest);
37
        Ok(())
38
    }
39
}
40
impl<R: rand_core::CryptoRng> rand_core_06::CryptoRng for RngCompat<R> {}
41

            
42
impl<R: rand_core_06::RngCore> rand_core::RngCore for RngCompat<R> {
43
    fn next_u32(&mut self) -> u32 {
44
        self.0.next_u32()
45
    }
46

            
47
    fn next_u64(&mut self) -> u64 {
48
        self.0.next_u64()
49
    }
50

            
51
    fn fill_bytes(&mut self, dest: &mut [u8]) {
52
        self.0.fill_bytes(dest);
53
    }
54
}
55

            
56
impl<R: rand_core_06::CryptoRng + rand_core_06::RngCore> rand_core::CryptoRng for RngCompat<R> {}