aboutsummaryrefslogtreecommitdiff
path: root/rand.rs
blob: 340defb2116442aae6bce20c48d6316e35334393 (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
import libc::{c_uchar, c_int};

#[link_name = "crypto"]
#[abi = "cdecl"]
extern mod libcrypto {
    fn RAND_bytes(buf: *c_uchar, num: c_int) -> c_int;
}

fn rand_bytes(len: uint) -> ~[u8] {
    let mut out = ~[];
    vec::reserve(out, len);

    do vec::as_buf(out) |out_buf| {
        let r = libcrypto::RAND_bytes(out_buf, len as c_int);
        if r != 1 as c_int { fail }

        unsafe { vec::unsafe::set_len(out, len); }
    }

    out
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_rand_bytes() {
        let _bytes = rand_bytes(5u);
    }
}