barnacl 0.1.1

Fast cryptographic library for Rust (bindings to libsodium)
Documentation
//! Cryptographic random number generation.
use ffi;
use libc::size_t;
use std::iter::repeat;

/// `randombytes()` randomly generates size bytes of data.
///
/// THREAD SAFETY: `randombytes()` is thread-safe provided that you have
/// called `barnacl::init()` once before using any other function
/// from barnacl.
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 as size_t);
        buf
    }
}

/// `randombytes_into()` fills a buffer `buf` with random data.
///
/// THREAD SAFETY: `randombytes_into()` is thread-safe provided that you have
/// called `barnacl::init()` once before using any other function
/// from barnacl.
pub fn randombytes_into(buf: &mut [u8]) {
    unsafe {
        ffi::randombytes_buf(buf.as_mut_ptr(), buf.len() as size_t);
    }
}