blob: 9ded0a76b162ba4d6ada1742befd8c6c4dccc39f (
plain) (
blame)
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
|
use std::libc::c_int;
use std::slice;
#[link(name = "crypto")]
extern {
fn RAND_bytes(buf: *mut u8, num: c_int) -> c_int;
}
pub fn rand_bytes(len: uint) -> ~[u8] {
unsafe {
let mut out = slice::with_capacity(len);
let r = RAND_bytes(out.as_mut_ptr(), len as c_int);
if r != 1 as c_int { fail!() }
out.set_len(len);
out
}
}
#[cfg(test)]
mod tests {
use super::rand_bytes;
#[test]
fn test_rand_bytes() {
let bytes = rand_bytes(32u);
println!("{:?}", bytes);
}
}
|