aboutsummaryrefslogtreecommitdiff
path: root/hash.rs
diff options
context:
space:
mode:
authorErick Tryzelaar <[email protected]>2012-09-27 23:32:12 -0700
committerErick Tryzelaar <[email protected]>2012-09-27 23:32:12 -0700
commitfb8f201b97a570ae81d033695635d8c0cc531849 (patch)
treee99a7d2ce3a654fc194d7c5f79172d2b6120bc30 /hash.rs
parentAdd a .gitignore (diff)
downloadrust-openssl-fb8f201b97a570ae81d033695635d8c0cc531849.tar.xz
rust-openssl-fb8f201b97a570ae81d033695635d8c0cc531849.zip
Update to rust HEAD
Diffstat (limited to 'hash.rs')
-rw-r--r--hash.rs137
1 files changed, 63 insertions, 74 deletions
diff --git a/hash.rs b/hash.rs
index 693c8223..6e0af07a 100644
--- a/hash.rs
+++ b/hash.rs
@@ -1,36 +1,18 @@
-import libc::c_uint;
-
-export hasher;
-export hashtype;
-export hash;
-export libcrypto;
-
-export md5, sha1, sha224, sha256, sha384, sha512;
-
-iface hasher {
- #[doc = "Initializes this hasher"]
- fn init();
-
- #[doc = "Update this hasher with more input bytes"]
- fn update(~[u8]);
-
- #[doc = "
- Return the digest of all bytes added to this hasher since its last
- initialization
- "]
- fn final() -> ~[u8];
-}
-
-enum hashtype {
- md5,
- sha1,
- sha224,
- sha256,
- sha384,
- sha512
+use libc::c_uint;
+
+pub enum HashType {
+ MD5,
+ SHA1,
+ SHA224,
+ SHA256,
+ SHA384,
+ SHA512
}
+#[allow(non_camel_case_types)]
type EVP_MD_CTX = *libc::c_void;
+
+#[allow(non_camel_case_types)]
type EVP_MD = *libc::c_void;
#[link_name = "crypto"]
@@ -47,61 +29,68 @@ extern mod libcrypto {
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: *u8, n: *u32);
+ fn EVP_DigestFinal(ctx: EVP_MD_CTX, res: *mut u8, n: *u32);
}
-fn evpmd(t: hashtype) -> (EVP_MD, uint) {
- alt 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) }
+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),
}
}
-fn hasher(ht: hashtype) -> hasher {
- type hasherstate = {
- evp: EVP_MD,
- ctx: EVP_MD_CTX,
- len: uint
- };
+pub struct Hasher {
+ priv evp: EVP_MD,
+ priv ctx: EVP_MD_CTX,
+ priv len: uint,
+}
- impl of hasher for hasherstate {
- fn init() unsafe {
- libcrypto::EVP_DigestInit(self.ctx, self.evp);
- }
+pub fn Hasher(ht: HashType) -> Hasher {
+ let ctx = libcrypto::EVP_MD_CTX_create();
+ let (evp, mdlen) = evpmd(ht);
+ let h = Hasher { evp: evp, ctx: ctx, len: mdlen };
+ h.init();
+ h
+}
- fn update(data: ~[u8]) unsafe {
- let pdata: *u8 = vec::unsafe::to_ptr::<u8>(data);
- libcrypto::EVP_DigestUpdate(self.ctx, pdata, vec::len(data) as c_uint);
- }
+pub impl Hasher {
+ /// Initializes this hasher
+ fn init() unsafe {
+ libcrypto::EVP_DigestInit(self.ctx, self.evp);
+ }
- fn final() -> ~[u8] unsafe {
- let res = vec::to_mut(vec::from_elem::<u8>(self.len, 0u8));
- let pres = vec::unsafe::to_ptr::<u8>(res);
- libcrypto::EVP_DigestFinal(self.ctx, pres, ptr::null::<u32>());
- vec::from_mut::<u8>(res)
+ /// 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)
}
}
- let ctx = libcrypto::EVP_MD_CTX_create();
- let (evp, mdlen) = evpmd(ht);
- let st = { evp: evp, ctx: ctx, len: mdlen };
- let h = st as hasher;
- h.init();
- ret h;
+ /**
+ * Return the digest of all bytes added to this hasher since its last
+ * initialization
+ */
+ fn final() -> ~[u8] unsafe {
+ 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());
+ }
+ res
+ }
}
-#[doc = "
-Hashes the supplied input data using hash t, returning the resulting hash value
-"]
-fn hash(t: hashtype, data: ~[u8]) -> ~[u8] unsafe {
- let h = hasher(t);
- h.init();
+/**
+ * Hashes the supplied input data using hash t, returning the resulting hash
+ * value
+ */
+pub fn hash(t: HashType, data: &[u8]) -> ~[u8] unsafe {
+ let h = Hasher(t);
h.update(data);
- ret h.final();
+ h.final()
}
#[cfg(test)]
@@ -113,7 +102,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]
@@ -123,7 +112,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]
@@ -134,6 +123,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);
}
}