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

Commit

Permalink
Implement Write for HmacEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
elsirion committed Jul 15, 2021
1 parent 28d8f4f commit f1084bf
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::{error, io};
#[cfg(not(feature = "std"))]
use core2::{error, io};

use {hex, sha1, sha256, sha512, ripemd160, siphash24};
use {hex, sha1, sha256, sha512, ripemd160, siphash24, hmac};
use HashEngine;
use Error;

Expand Down Expand Up @@ -85,11 +85,20 @@ impl io::Write for siphash24::HashEngine {
}
}

impl<T: ::Hash> io::Write for hmac::HmacEngine<T> {
fn flush(&mut self) -> io::Result<()> { Ok(()) }

fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.input(buf);
Ok(buf.len())
}
}

#[cfg(test)]
mod tests {
use super::io::Write;

use {sha1, sha256, sha256d, sha512, ripemd160, hash160, siphash24};
use {sha1, sha256, sha256d, sha512, ripemd160, hash160, siphash24, hmac};
use Hash;

macro_rules! write_test {
Expand Down Expand Up @@ -171,4 +180,28 @@ mod tests {
"3a3ccefde9b5b1e3",
"ce456e4e4ecbc5bf",
);

#[test]
fn hmac() {
let mut engine = hmac::HmacEngine::<sha256::Hash>::new(&[0xde, 0xad, 0xbe, 0xef]);
engine.write_all(&[]).unwrap();
assert_eq!(
format!("{}", hmac::Hmac::from_engine(engine)),
"bf5515149cf797955c4d3194cca42472883281951697c8375d9d9b107f384225"
);

let mut engine = hmac::HmacEngine::<sha256::Hash>::new(&[0xde, 0xad, 0xbe, 0xef]);
engine.write_all(&[1; 256]).unwrap();
assert_eq!(
format!("{}", hmac::Hmac::from_engine(engine)),
"59c9aca10c81c73cb4c196d94db741b6bf2050e0153d5a45f2526bff34675ac5"
);

let mut engine = hmac::HmacEngine::<sha256::Hash>::new(&[0xde, 0xad, 0xbe, 0xef]);
engine.write_all(&[99; 64000]).unwrap();
assert_eq!(
format!("{}", hmac::Hmac::from_engine(engine)),
"30df499717415a395379a1eaabe50038036e4abb5afc94aa55c952f4aa57be08"
);
}
}

0 comments on commit f1084bf

Please sign in to comment.