diff options
| author | Erick Tryzelaar <[email protected]> | 2013-08-15 07:15:35 -0700 |
|---|---|---|
| committer | Erick Tryzelaar <[email protected]> | 2013-08-15 07:15:35 -0700 |
| commit | e3fef0c40ec4d7a14221d8b1d39d85dc4ecf17d3 (patch) | |
| tree | 88e7b32365d5b4838ec0180c91e237c84e0210c3 /hash.rs | |
| parent | Expose the submodules (diff) | |
| download | rust-openssl-e3fef0c40ec4d7a14221d8b1d39d85dc4ecf17d3.tar.xz rust-openssl-e3fef0c40ec4d7a14221d8b1d39d85dc4ecf17d3.zip | |
Update to rust 0.8-pre
Diffstat (limited to 'hash.rs')
| -rw-r--r-- | hash.rs | 95 |
1 files changed, 55 insertions, 40 deletions
@@ -1,4 +1,7 @@ -use libc::c_uint; +use std::libc::c_uint; +use std::libc; +use std::ptr; +use std::vec; pub enum HashType { MD5, @@ -10,36 +13,42 @@ pub enum HashType { } #[allow(non_camel_case_types)] -type EVP_MD_CTX = *libc::c_void; +pub type EVP_MD_CTX = *libc::c_void; #[allow(non_camel_case_types)] -type EVP_MD = *libc::c_void; - -#[link_name = "crypto"] -#[abi = "cdecl"] -extern mod libcrypto { - fn EVP_MD_CTX_create() -> EVP_MD_CTX; - - fn EVP_md5() -> EVP_MD; - fn EVP_sha1() -> EVP_MD; - fn EVP_sha224() -> EVP_MD; - fn EVP_sha256() -> EVP_MD; - fn EVP_sha384() -> EVP_MD; - fn EVP_sha512() -> EVP_MD; - - fn EVP_DigestInit(ctx: EVP_MD_CTX, typ: EVP_MD); - fn EVP_DigestUpdate(ctx: EVP_MD_CTX, data: *u8, n: c_uint); - fn EVP_DigestFinal(ctx: EVP_MD_CTX, res: *mut u8, n: *u32); +pub type EVP_MD = *libc::c_void; + +mod libcrypto { + use super::*; + use std::libc::c_uint; + + #[link_args = "-lcrypto"] + extern { + fn EVP_MD_CTX_create() -> EVP_MD_CTX; + + fn EVP_md5() -> EVP_MD; + fn EVP_sha1() -> EVP_MD; + fn EVP_sha224() -> EVP_MD; + fn EVP_sha256() -> EVP_MD; + fn EVP_sha384() -> EVP_MD; + fn EVP_sha512() -> EVP_MD; + + fn EVP_DigestInit(ctx: EVP_MD_CTX, typ: EVP_MD); + fn EVP_DigestUpdate(ctx: EVP_MD_CTX, data: *u8, n: c_uint); + fn EVP_DigestFinal(ctx: EVP_MD_CTX, res: *mut u8, n: *u32); + } } fn evpmd(t: HashType) -> (EVP_MD, uint) { - match t { - MD5 => (libcrypto::EVP_md5(), 16u), - SHA1 => (libcrypto::EVP_sha1(), 20u), - SHA224 => (libcrypto::EVP_sha224(), 28u), - SHA256 => (libcrypto::EVP_sha256(), 32u), - SHA384 => (libcrypto::EVP_sha384(), 48u), - SHA512 => (libcrypto::EVP_sha512(), 64u), + unsafe { + match t { + MD5 => (libcrypto::EVP_md5(), 16u), + SHA1 => (libcrypto::EVP_sha1(), 20u), + SHA224 => (libcrypto::EVP_sha224(), 28u), + SHA256 => (libcrypto::EVP_sha256(), 32u), + SHA384 => (libcrypto::EVP_sha384(), 48u), + SHA512 => (libcrypto::EVP_sha512(), 64u), + } } } @@ -50,23 +59,25 @@ pub struct Hasher { } pub fn Hasher(ht: HashType) -> Hasher { - let ctx = libcrypto::EVP_MD_CTX_create(); + let ctx = unsafe { libcrypto::EVP_MD_CTX_create() }; let (evp, mdlen) = evpmd(ht); let h = Hasher { evp: evp, ctx: ctx, len: mdlen }; h.init(); h } -pub impl Hasher { +impl Hasher { /// Initializes this hasher - fn init() unsafe { - libcrypto::EVP_DigestInit(self.ctx, self.evp); + pub fn init(&self) { + unsafe { libcrypto::EVP_DigestInit(self.ctx, self.evp) } } /// Update this hasher with more input bytes - fn update(data: &[u8]) unsafe { - do vec::as_imm_buf(data) |pdata, len| { - libcrypto::EVP_DigestUpdate(self.ctx, pdata, len as c_uint) + pub fn update(&self, data: &[u8]) { + do data.as_imm_buf |pdata, len| { + unsafe { + libcrypto::EVP_DigestUpdate(self.ctx, pdata, len as c_uint) + } } } @@ -74,10 +85,12 @@ pub impl Hasher { * Return the digest of all bytes added to this hasher since its last * initialization */ - fn final() -> ~[u8] unsafe { + pub fn final(&self) -> ~[u8] { let mut res = vec::from_elem(self.len, 0u8); - do vec::as_mut_buf(res) |pres, _len| { - libcrypto::EVP_DigestFinal(self.ctx, pres, ptr::null()); + do res.as_mut_buf |pres, _len| { + unsafe { + libcrypto::EVP_DigestFinal(self.ctx, pres, ptr::null()); + } } res } @@ -87,7 +100,7 @@ pub impl Hasher { * Hashes the supplied input data using hash t, returning the resulting hash * value */ -pub fn hash(t: HashType, data: &[u8]) -> ~[u8] unsafe { +pub fn hash(t: HashType, data: &[u8]) -> ~[u8] { let h = Hasher(t); h.update(data); h.final() @@ -95,6 +108,8 @@ pub fn hash(t: HashType, data: &[u8]) -> ~[u8] unsafe { #[cfg(test)] mod tests { + use super::*; + // Test vectors from http://www.nsrl.nist.gov/testdata/ #[test] fn test_md5() { @@ -102,7 +117,7 @@ mod tests { let d0 = ~[0x90u8, 0x01u8, 0x50u8, 0x98u8, 0x3cu8, 0xd2u8, 0x4fu8, 0xb0u8, 0xd6u8, 0x96u8, 0x3fu8, 0x7du8, 0x28u8, 0xe1u8, 0x7fu8, 0x72u8]; - assert(hash(MD5, s0) == d0); + assert!(hash(MD5, s0) == d0); } #[test] @@ -112,7 +127,7 @@ mod tests { ~[0xa9u8, 0x99u8, 0x3eu8, 0x36u8, 0x47u8, 0x06u8, 0x81u8, 0x6au8, 0xbau8, 0x3eu8, 0x25u8, 0x71u8, 0x78u8, 0x50u8, 0xc2u8, 0x6cu8, 0x9cu8, 0xd0u8, 0xd8u8, 0x9du8]; - assert(hash(SHA1, s0) == d0); + assert!(hash(SHA1, s0) == d0); } #[test] @@ -123,6 +138,6 @@ mod tests { 0x41u8, 0x41u8, 0x40u8, 0xdeu8, 0x5du8, 0xaeu8, 0x22u8, 0x23u8, 0xb0u8, 0x03u8, 0x61u8, 0xa3u8, 0x96u8, 0x17u8, 0x7au8, 0x9cu8, 0xb4u8, 0x10u8, 0xffu8, 0x61u8, 0xf2u8, 0x00u8, 0x15u8, 0xadu8]; - assert(hash(SHA256, s0) == d0); + assert!(hash(SHA256, s0) == d0); } } |