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

Implement Write for HmacEngine #133

Merged
merged 1 commit into from
Sep 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
);
}
}