diff options
| author | Steven Fackler <[email protected]> | 2014-08-03 19:16:09 -0700 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2014-08-03 19:16:09 -0700 |
| commit | 203bdd076ec744a1794a7b151efb6b9247d43455 (patch) | |
| tree | 8b4e69b8bdfd834648b73243519f4eeca301bb90 /src/crypto/rand.rs | |
| parent | Remove Makefile infrastructure (diff) | |
| download | rust-openssl-203bdd076ec744a1794a7b151efb6b9247d43455.tar.xz rust-openssl-203bdd076ec744a1794a7b151efb6b9247d43455.zip | |
Shift directory structure
Diffstat (limited to 'src/crypto/rand.rs')
| -rw-r--r-- | src/crypto/rand.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/crypto/rand.rs b/src/crypto/rand.rs new file mode 100644 index 00000000..9db87fcd --- /dev/null +++ b/src/crypto/rand.rs @@ -0,0 +1,30 @@ +use libc::c_int; + +#[link(name = "crypto")] +extern { + fn RAND_bytes(buf: *mut u8, num: c_int) -> c_int; +} + +pub fn rand_bytes(len: uint) -> Vec<u8> { + unsafe { + let mut out = Vec::with_capacity(len); + + let r = RAND_bytes(out.as_mut_ptr(), len as c_int); + if r != 1 as c_int { fail!() } + + out.set_len(len); + + out + } +} + +#[cfg(test)] +mod tests { + use super::rand_bytes; + + #[test] + fn test_rand_bytes() { + let bytes = rand_bytes(32u); + println!("{}", bytes); + } +} |