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
//! Cryptographic random number generation. use ffi; #[cfg(not(feature = "std"))] use prelude::*; /// `randombytes()` randomly generates size bytes of data. /// /// THREAD SAFETY: `randombytes()` is thread-safe provided that you have /// called `sodiumoxide::init()` once before using any other function /// from sodiumoxide. pub fn randombytes(size: usize) -> Vec<u8> { unsafe { let mut buf = vec![0u8; size]; ffi::randombytes_buf(buf.as_mut_ptr() as *mut _, size); buf } } /// `randombytes_into()` fills a buffer `buf` with random data. /// /// THREAD SAFETY: `randombytes_into()` is thread-safe provided that you have /// called `sodiumoxide::init()` once before using any other function /// from sodiumoxide. pub fn randombytes_into(buf: &mut [u8]) { unsafe { ffi::randombytes_buf(buf.as_mut_ptr() as *mut _, buf.len()); } } /// `randombytes_uniform()` returns an unpredictable value between 0 and /// `upper_bound` (excluded). It guarantees a uniform distribution of the /// possible output values even when `upper_bound` is not a power of 2. Note /// that an `upper_bound` < 2 leaves only a single element to be chosen, namely /// 0. /// /// THREAD SAFETY: `randombytes()` is thread-safe provided that you have /// called `sodiumoxide::init()` once before using any other function /// from sodiumoxide. pub fn randombytes_uniform(upper_bound: u32) -> u32 { unsafe { ffi::randombytes_uniform(upper_bound) } } #[cfg(test)] mod test { use super::*; #[test] fn test_randombytes_uniform_0() { ::init().unwrap(); assert_eq!(randombytes_uniform(0), 0); } #[test] fn test_randombytes_uniform_1() { ::init().unwrap(); assert_eq!(randombytes_uniform(1), 0); } #[test] fn test_randombytes_uniform_7() { ::init().unwrap(); assert!(randombytes_uniform(7) < 7); } }