milagro_crypto/randapi/
mod.rs

1#![allow(non_camel_case_types)]
2#![allow(non_upper_case_globals)]
3
4pub mod wrappers;
5
6extern crate libc;
7use self::libc::{c_int};
8
9use randapi::wrappers::*;
10
11pub struct Random {
12    pub rng: csprng
13}
14
15impl Random {
16    pub fn new(seed: &Vec<u8>) -> Random {
17        let mut ret: csprng = csprng::new();
18        let mut o = octet::new(&seed.as_slice(), seed.len());
19        unsafe {
20            CREATE_CSPRNG(&mut ret, &mut o);
21        }
22        Random {
23            rng: ret
24        }
25    }
26
27    pub fn getbyte(&mut self) -> u8 {
28        let r: c_int;
29        unsafe {
30            r = RAND_byte(&mut self.rng);
31        }
32        return r as u8;
33    }
34}
35
36impl Drop for Random {
37    fn drop(&mut self) {
38        unsafe {
39            KILL_CSPRNG(&mut self.rng);
40        }
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_random() {
50        let seed: Vec<u8> = vec![ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51                                  0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
52                                  0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53                                  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 ];
54        Random::new(&seed);
55        // no assert, segfault means test failed
56    }
57}