aboutsummaryrefslogtreecommitdiff
path: root/src/crypto/rand.rs
diff options
context:
space:
mode:
authorErick Tryzelaar <[email protected]>2013-12-18 08:51:10 -0800
committerErick Tryzelaar <[email protected]>2013-12-18 08:51:10 -0800
commita9ce2a36d54ac08608d689a5f0ec441f74e56d61 (patch)
tree813ceb833817cfac21a1cc920446f8a7927acf76 /src/crypto/rand.rs
parentUpdate to rust 0.9x-pre (diff)
downloadrust-openssl-a9ce2a36d54ac08608d689a5f0ec441f74e56d61.tar.xz
rust-openssl-a9ce2a36d54ac08608d689a5f0ec441f74e56d61.zip
Switch over to rustpkg
Diffstat (limited to 'src/crypto/rand.rs')
-rw-r--r--src/crypto/rand.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/crypto/rand.rs b/src/crypto/rand.rs
new file mode 100644
index 00000000..6510b9f4
--- /dev/null
+++ b/src/crypto/rand.rs
@@ -0,0 +1,35 @@
+use std::libc::c_int;
+use std::vec;
+
+mod libcrypto {
+ use std::libc::c_int;
+
+ #[link(name = "crypto")]
+ extern {
+ pub fn RAND_bytes(buf: *mut u8, num: c_int) -> c_int;
+ }
+}
+
+pub fn rand_bytes(len: uint) -> ~[u8] {
+ let mut out = vec::with_capacity(len);
+
+ out.as_mut_buf(|out_buf, len| {
+ let r = unsafe { libcrypto::RAND_bytes(out_buf, len as c_int) };
+ if r != 1 as c_int { fail!() }
+ });
+
+ unsafe { out.set_len(len); }
+
+ out
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_rand_bytes() {
+ let bytes = rand_bytes(32u);
+ println!("{:?}", bytes);
+ }
+}