aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSteven Fackler <[email protected]>2014-11-07 14:24:23 -0800
committerSteven Fackler <[email protected]>2014-11-07 14:24:23 -0800
commit766ce4b7785c0b038bee95b74564e5b9bf7e9a4b (patch)
tree1d7083db3cf61f1261dc867c13ffdf6cc5c5b59c /src
parentMerge pull request #93 from jmesmon/fix-tcpstream (diff)
parentHasher::write(): add basic test (diff)
downloadrust-openssl-766ce4b7785c0b038bee95b74564e5b9bf7e9a4b.tar.xz
rust-openssl-766ce4b7785c0b038bee95b74564e5b9bf7e9a4b.zip
Merge pull request #92 from jmesmon/hash-writier
crypto/hash: impl Writer for Hasher to allow use of Reader-Writer convenience functions
Diffstat (limited to 'src')
-rw-r--r--src/crypto/hash.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/crypto/hash.rs b/src/crypto/hash.rs
index a72b8d9f..b00dfba8 100644
--- a/src/crypto/hash.rs
+++ b/src/crypto/hash.rs
@@ -1,5 +1,6 @@
use libc::c_uint;
use std::ptr;
+use std::io;
use ffi;
@@ -34,6 +35,13 @@ pub struct Hasher {
len: uint,
}
+impl io::Writer for Hasher {
+ fn write(&mut self, buf: &[u8]) -> io::IoResult<()> {
+ self.update(buf);
+ Ok(())
+ }
+}
+
impl Hasher {
pub fn new(ht: HashType) -> Hasher {
ffi::init();
@@ -112,6 +120,12 @@ mod tests {
assert!(calced == hashtest.expected_output);
}
+ pub fn hash_writer(t: super::HashType, data: &[u8]) -> Vec<u8> {
+ let mut h = super::Hasher::new(t);
+ h.write(data);
+ h.finalize()
+ }
+
// Test vectors from http://www.nsrl.nist.gov/testdata/
#[test]
fn test_md5() {
@@ -167,4 +181,11 @@ mod tests {
hash_test(super::RIPEMD160, test);
}
}
+
+ #[test]
+ fn test_writer() {
+ let tv = "rust-openssl".as_bytes();
+ let ht = super::RIPEMD160;
+ assert!(hash_writer(ht, tv) == super::hash(ht, tv));
+ }
}