diff options
| author | Elly Jones <[email protected]> | 2012-01-18 21:46:39 -0500 |
|---|---|---|
| committer | Elly Jones <[email protected]> | 2012-01-18 21:46:39 -0500 |
| commit | 3ef626596ef88f3c8d0545ae0063734e32cfcebb (patch) | |
| tree | f4fbe84868cf7f1198095e57077f4f36feae38a4 | |
| parent | manifest: Add license. (diff) | |
| download | rust-openssl-3ef626596ef88f3c8d0545ae0063734e32cfcebb.tar.xz rust-openssl-3ef626596ef88f3c8d0545ae0063734e32cfcebb.zip | |
[hash] update to 2286d8c17e5bf3d4312357eba9a845262068f007
| -rw-r--r-- | hash.rs | 43 |
1 files changed, 17 insertions, 26 deletions
@@ -1,7 +1,11 @@ +use std; + import core::ptr; import core::str; import core::vec; +import ctypes::c_uint; + export hasher; export hashtype; export mk_hasher; @@ -10,7 +14,7 @@ export _native; export md5, sha1, sha224, sha256, sha384, sha512; -type hasher = obj { +iface hasher { /* Method: init @@ -32,7 +36,7 @@ type hasher = obj { initialization */ fn final() -> [u8]; -}; +} tag hashtype { md5; @@ -59,7 +63,7 @@ native mod _native { 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: uint); + fn EVP_DigestUpdate(ctx: EVP_MD_CTX, data: *u8, n: c_uint); fn EVP_DigestFinal(ctx: EVP_MD_CTX, res: *u8, n: *u32); } @@ -81,20 +85,20 @@ fn mk_hasher(ht: hashtype) -> hasher { len: uint }; - obj hasher(st: hasherstate) { + impl of hasher for hasherstate { fn init() unsafe { - _native::EVP_DigestInit(st.ctx, st.evp); + _native::EVP_DigestInit(self.ctx, self.evp); } fn update(data: [u8]) unsafe { let pdata: *u8 = vec::unsafe::to_ptr::<u8>(data); - _native::EVP_DigestUpdate(st.ctx, pdata, vec::len(data)); + _native::EVP_DigestUpdate(self.ctx, pdata, vec::len(data) as c_uint); } fn final() -> [u8] unsafe { - let res: [mutable u8] = vec::init_elt_mut::<u8>(0u8, st.len); + let res: [mutable u8] = vec::init_elt_mut::<u8>(0u8, self.len); let pres: *u8 = vec::unsafe::to_ptr::<u8>(res); - _native::EVP_DigestFinal(st.ctx, pres, ptr::null::<u32>()); + _native::EVP_DigestFinal(self.ctx, pres, ptr::null::<u32>()); vec::from_mut::<u8>(res) } } @@ -102,7 +106,7 @@ fn mk_hasher(ht: hashtype) -> hasher { let ctx = _native::EVP_MD_CTX_create(); let (evp, mdlen) = evpmd(ht); let st = { evp: evp, ctx: ctx, len: mdlen }; - let h = hasher(st); + let h = st as hasher; h.init(); ret h; } @@ -113,15 +117,10 @@ Function: hash Hashes the supplied input data using hash t, returning the resulting hash value */ fn hash(t: hashtype, data: [u8]) -> [u8] unsafe { - let ctx = _native::EVP_MD_CTX_create(); - let (evp, mdlen) = evpmd(t); - let res: [mutable u8] = vec::init_elt_mut::<u8>(0u8, mdlen); - let pres: *u8 = vec::unsafe::to_ptr::<u8>(res); - let pdata: *u8 = vec::unsafe::to_ptr::<u8>(data); - _native::EVP_DigestInit(ctx, evp); - _native::EVP_DigestUpdate(ctx, pdata, vec::len(data)); - _native::EVP_DigestFinal(ctx, pres, ptr::null::<u32>()); - ret vec::from_mut::<u8>(res); + let h = mk_hasher(t); + h.init(); + h.update(data); + ret h.final(); } #[cfg(test)] @@ -157,11 +156,3 @@ mod tests { assert(hash(sha256, s0) == d0); } } - -fn main() { - let h = mk_hasher(sha512); - h.init(); - h.update(str::bytes("")); - log h.final(); - log hash(sha512, str::bytes("")); -} |