diff options
| author | Erick Tryzelaar <[email protected]> | 2012-04-21 20:03:06 -0700 |
|---|---|---|
| committer | Erick Tryzelaar <[email protected]> | 2012-04-21 20:03:06 -0700 |
| commit | b2f3316758ebbf7da78417675f6b208a5386040f (patch) | |
| tree | a967b91c8fe25acc3bd45913a437d7c7fb9d549b | |
| parent | Add support for PKCS5_PBKDF2_HMAC_SHA1. (diff) | |
| download | rust-openssl-b2f3316758ebbf7da78417675f6b208a5386040f.tar.xz rust-openssl-b2f3316758ebbf7da78417675f6b208a5386040f.zip | |
Expose RAND_bytes
| -rw-r--r-- | crypto.rc | 1 | ||||
| -rw-r--r-- | rand.rs | 28 |
2 files changed, 29 insertions, 0 deletions
@@ -25,3 +25,4 @@ mod hash; mod pkey; mod symm; mod pkcs5; +mod rand; diff --git a/rand.rs b/rand.rs new file mode 100644 index 00000000..7335b51a --- /dev/null +++ b/rand.rs @@ -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); + } +} |