Add a Hmac::engine function
The `HmacEngine` cannot be constructed by way of the `GeneralHash` trait method because it requires a key. However we can still add an inherent function to the type to construct an engine. Add the engine constructor and use it in bench code.
This commit is contained in:
parent
c352d376ed
commit
1eb8f1f9e0
|
@ -19,6 +19,16 @@ use crate::{FromSliceError, GeneralHash, Hash, HashEngine};
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
pub struct Hmac<T: GeneralHash>(T);
|
pub struct Hmac<T: GeneralHash>(T);
|
||||||
|
|
||||||
|
impl<T: GeneralHash> Hmac<T> {
|
||||||
|
/// Constructs a new keyed HMAC engine from `key`.
|
||||||
|
pub fn engine(key: &[u8]) -> HmacEngine<T>
|
||||||
|
where
|
||||||
|
<T as GeneralHash>::Engine: Default,
|
||||||
|
{
|
||||||
|
HmacEngine::new(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: GeneralHash + str::FromStr> str::FromStr for Hmac<T> {
|
impl<T: GeneralHash + str::FromStr> str::FromStr for Hmac<T> {
|
||||||
type Err = <T as str::FromStr>::Err;
|
type Err = <T as str::FromStr>::Err;
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> { Ok(Hmac(str::FromStr::from_str(s)?)) }
|
fn from_str(s: &str) -> Result<Self, Self::Err> { Ok(Hmac(str::FromStr::from_str(s)?)) }
|
||||||
|
@ -32,7 +42,7 @@ pub struct HmacEngine<T: GeneralHash> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: GeneralHash> HmacEngine<T> {
|
impl<T: GeneralHash> HmacEngine<T> {
|
||||||
/// Constructs a new keyed HMAC from `key`.
|
/// Constructs a new keyed HMAC engine from `key`.
|
||||||
///
|
///
|
||||||
/// We only support underlying hashes whose block sizes are ≤ 128 bytes.
|
/// We only support underlying hashes whose block sizes are ≤ 128 bytes.
|
||||||
///
|
///
|
||||||
|
@ -321,7 +331,7 @@ mod benches {
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
pub fn hmac_sha256_10(bh: &mut Bencher) {
|
pub fn hmac_sha256_10(bh: &mut Bencher) {
|
||||||
let mut engine = HmacEngine::<sha256::Hashe>::new(&[]);
|
let mut engine = Hmac::<sha256::Hash>::engine(&[]);
|
||||||
let bytes = [1u8; 10];
|
let bytes = [1u8; 10];
|
||||||
bh.iter(|| {
|
bh.iter(|| {
|
||||||
engine.input(&bytes);
|
engine.input(&bytes);
|
||||||
|
@ -331,7 +341,7 @@ mod benches {
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
pub fn hmac_sha256_1k(bh: &mut Bencher) {
|
pub fn hmac_sha256_1k(bh: &mut Bencher) {
|
||||||
let mut engine = HmacEngine::<sha256::Hashe>::new(&[]);
|
let mut engine = Hmac::<sha256::Hash>::engine(&[]);
|
||||||
let bytes = [1u8; 1024];
|
let bytes = [1u8; 1024];
|
||||||
bh.iter(|| {
|
bh.iter(|| {
|
||||||
engine.input(&bytes);
|
engine.input(&bytes);
|
||||||
|
@ -341,7 +351,7 @@ mod benches {
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
pub fn hmac_sha256_64k(bh: &mut Bencher) {
|
pub fn hmac_sha256_64k(bh: &mut Bencher) {
|
||||||
let mut engine = HmacEngine::<sha256::Hashe>::new(&[]);
|
let mut engine = Hmac::<sha256::Hash>::engine(&[]);
|
||||||
let bytes = [1u8; 65536];
|
let bytes = [1u8; 65536];
|
||||||
bh.iter(|| {
|
bh.iter(|| {
|
||||||
engine.input(&bytes);
|
engine.input(&bytes);
|
||||||
|
|
Loading…
Reference in New Issue