Skip to content

Commit

Permalink
Expose RAND_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
erickt committed Apr 22, 2012
1 parent 6794c17 commit b2f3316
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions crypto.rc
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ mod hash;
mod pkey;
mod symm;
mod pkcs5;
mod rand;
28 changes: 28 additions & 0 deletions rand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import libc::{c_uchar, c_int};

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

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

vec::as_buf(out) { |out_buf|
let r = _native::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);
}
}

0 comments on commit b2f3316

Please sign in to comment.