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
//! Cryptographic random number generation. use ffi; use std::iter::repeat; /// `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<u8> = repeat(0u8).take(size).collect(); let pbuf = buf.as_mut_ptr(); ffi::randombytes_buf(pbuf, 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(), buf.len()); } }