Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit f1084bf

Browse files
committed
Implement Write for HmacEngine
1 parent 28d8f4f commit f1084bf

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/impls.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::{error, io};
2222
#[cfg(not(feature = "std"))]
2323
use core2::{error, io};
2424

25-
use {hex, sha1, sha256, sha512, ripemd160, siphash24};
25+
use {hex, sha1, sha256, sha512, ripemd160, siphash24, hmac};
2626
use HashEngine;
2727
use Error;
2828

@@ -85,11 +85,20 @@ impl io::Write for siphash24::HashEngine {
8585
}
8686
}
8787

88+
impl<T: ::Hash> io::Write for hmac::HmacEngine<T> {
89+
fn flush(&mut self) -> io::Result<()> { Ok(()) }
90+
91+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
92+
self.input(buf);
93+
Ok(buf.len())
94+
}
95+
}
96+
8897
#[cfg(test)]
8998
mod tests {
9099
use super::io::Write;
91100

92-
use {sha1, sha256, sha256d, sha512, ripemd160, hash160, siphash24};
101+
use {sha1, sha256, sha256d, sha512, ripemd160, hash160, siphash24, hmac};
93102
use Hash;
94103

95104
macro_rules! write_test {
@@ -171,4 +180,28 @@ mod tests {
171180
"3a3ccefde9b5b1e3",
172181
"ce456e4e4ecbc5bf",
173182
);
183+
184+
#[test]
185+
fn hmac() {
186+
let mut engine = hmac::HmacEngine::<sha256::Hash>::new(&[0xde, 0xad, 0xbe, 0xef]);
187+
engine.write_all(&[]).unwrap();
188+
assert_eq!(
189+
format!("{}", hmac::Hmac::from_engine(engine)),
190+
"bf5515149cf797955c4d3194cca42472883281951697c8375d9d9b107f384225"
191+
);
192+
193+
let mut engine = hmac::HmacEngine::<sha256::Hash>::new(&[0xde, 0xad, 0xbe, 0xef]);
194+
engine.write_all(&[1; 256]).unwrap();
195+
assert_eq!(
196+
format!("{}", hmac::Hmac::from_engine(engine)),
197+
"59c9aca10c81c73cb4c196d94db741b6bf2050e0153d5a45f2526bff34675ac5"
198+
);
199+
200+
let mut engine = hmac::HmacEngine::<sha256::Hash>::new(&[0xde, 0xad, 0xbe, 0xef]);
201+
engine.write_all(&[99; 64000]).unwrap();
202+
assert_eq!(
203+
format!("{}", hmac::Hmac::from_engine(engine)),
204+
"30df499717415a395379a1eaabe50038036e4abb5afc94aa55c952f4aa57be08"
205+
);
206+
}
174207
}

0 commit comments

Comments
 (0)