Merge pull request #92 from jmesmon/hash-writier
crypto/hash: impl Writer for Hasher to allow use of Reader-Writer convenience functions
This commit is contained in:
commit
766ce4b778
|
|
@ -1,5 +1,6 @@
|
||||||
use libc::c_uint;
|
use libc::c_uint;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
use std::io;
|
||||||
|
|
||||||
use ffi;
|
use ffi;
|
||||||
|
|
||||||
|
|
@ -34,6 +35,13 @@ pub struct Hasher {
|
||||||
len: uint,
|
len: uint,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl io::Writer for Hasher {
|
||||||
|
fn write(&mut self, buf: &[u8]) -> io::IoResult<()> {
|
||||||
|
self.update(buf);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Hasher {
|
impl Hasher {
|
||||||
pub fn new(ht: HashType) -> Hasher {
|
pub fn new(ht: HashType) -> Hasher {
|
||||||
ffi::init();
|
ffi::init();
|
||||||
|
|
@ -112,6 +120,12 @@ mod tests {
|
||||||
assert!(calced == hashtest.expected_output);
|
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 vectors from http://www.nsrl.nist.gov/testdata/
|
||||||
#[test]
|
#[test]
|
||||||
fn test_md5() {
|
fn test_md5() {
|
||||||
|
|
@ -167,4 +181,11 @@ mod tests {
|
||||||
hash_test(super::RIPEMD160, test);
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue