[hash] update to 2286d8c17e5bf3d4312357eba9a845262068f007

This commit is contained in:
Elly Jones 2012-01-18 21:46:39 -05:00
parent aa9c371f20
commit 3ef626596e
1 changed files with 17 additions and 26 deletions

43
hash.rs
View File

@ -1,7 +1,11 @@
use std;
import core::ptr; import core::ptr;
import core::str; import core::str;
import core::vec; import core::vec;
import ctypes::c_uint;
export hasher; export hasher;
export hashtype; export hashtype;
export mk_hasher; export mk_hasher;
@ -10,7 +14,7 @@ export _native;
export md5, sha1, sha224, sha256, sha384, sha512; export md5, sha1, sha224, sha256, sha384, sha512;
type hasher = obj { iface hasher {
/* /*
Method: init Method: init
@ -32,7 +36,7 @@ type hasher = obj {
initialization initialization
*/ */
fn final() -> [u8]; fn final() -> [u8];
}; }
tag hashtype { tag hashtype {
md5; md5;
@ -59,7 +63,7 @@ native mod _native {
fn EVP_sha512() -> EVP_MD; fn EVP_sha512() -> EVP_MD;
fn EVP_DigestInit(ctx: EVP_MD_CTX, typ: 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); fn EVP_DigestFinal(ctx: EVP_MD_CTX, res: *u8, n: *u32);
} }
@ -81,20 +85,20 @@ fn mk_hasher(ht: hashtype) -> hasher {
len: uint len: uint
}; };
obj hasher(st: hasherstate) { impl of hasher for hasherstate {
fn init() unsafe { fn init() unsafe {
_native::EVP_DigestInit(st.ctx, st.evp); _native::EVP_DigestInit(self.ctx, self.evp);
} }
fn update(data: [u8]) unsafe { fn update(data: [u8]) unsafe {
let pdata: *u8 = vec::unsafe::to_ptr::<u8>(data); 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 { 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); 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) vec::from_mut::<u8>(res)
} }
} }
@ -102,7 +106,7 @@ fn mk_hasher(ht: hashtype) -> hasher {
let ctx = _native::EVP_MD_CTX_create(); let ctx = _native::EVP_MD_CTX_create();
let (evp, mdlen) = evpmd(ht); let (evp, mdlen) = evpmd(ht);
let st = { evp: evp, ctx: ctx, len: mdlen }; let st = { evp: evp, ctx: ctx, len: mdlen };
let h = hasher(st); let h = st as hasher;
h.init(); h.init();
ret h; ret h;
} }
@ -113,15 +117,10 @@ Function: hash
Hashes the supplied input data using hash t, returning the resulting hash value Hashes the supplied input data using hash t, returning the resulting hash value
*/ */
fn hash(t: hashtype, data: [u8]) -> [u8] unsafe { fn hash(t: hashtype, data: [u8]) -> [u8] unsafe {
let ctx = _native::EVP_MD_CTX_create(); let h = mk_hasher(t);
let (evp, mdlen) = evpmd(t); h.init();
let res: [mutable u8] = vec::init_elt_mut::<u8>(0u8, mdlen); h.update(data);
let pres: *u8 = vec::unsafe::to_ptr::<u8>(res); ret h.final();
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);
} }
#[cfg(test)] #[cfg(test)]
@ -157,11 +156,3 @@ mod tests {
assert(hash(sha256, s0) == d0); 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(""));
}