aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErick Tryzelaar <[email protected]>2012-04-21 20:03:06 -0700
committerErick Tryzelaar <[email protected]>2012-04-21 20:03:06 -0700
commitb2f3316758ebbf7da78417675f6b208a5386040f (patch)
treea967b91c8fe25acc3bd45913a437d7c7fb9d549b
parentAdd support for PKCS5_PBKDF2_HMAC_SHA1. (diff)
downloadrust-openssl-b2f3316758ebbf7da78417675f6b208a5386040f.tar.xz
rust-openssl-b2f3316758ebbf7da78417675f6b208a5386040f.zip
Expose RAND_bytes
-rw-r--r--crypto.rc1
-rw-r--r--rand.rs28
2 files changed, 29 insertions, 0 deletions
diff --git a/crypto.rc b/crypto.rc
index cdd25fb8..4b0d6306 100644
--- a/crypto.rc
+++ b/crypto.rc
@@ -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);
+ }
+}