Skip to content

Commit 1e2292b

Browse files
committed
Add support for the digest 0.11 traits
1 parent 0ef6f41 commit 1e2292b

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hmac-sha1-compact"
3-
version = "1.1.6"
3+
version = "1.1.7"
44
authors = ["Frank Denis <github@pureftpd.org>"]
55
edition = "2018"
66
description = "A small, self-contained SHA1 and HMAC-SHA1 implementation"
@@ -13,12 +13,14 @@ license = "ISC"
1313
[dependencies]
1414
digest09 = { package = "digest", version = "0.9.0", optional = true }
1515
digest010 = { package = "digest", version = "0.10.7", features = ["oid"], optional = true }
16+
digest011 = { package = "digest", version = "0.11.0-pre.9", features = ["oid"], optional = true }
1617

1718
[features]
1819
default = []
19-
traits = ["traits09", "traits010"]
20+
traits = ["traits09", "traits010", "traits011"]
2021
traits09 = ["digest09"]
2122
traits010 = ["digest010"]
23+
traits011 = ["digest011"]
2224

2325
[profile.release]
2426
lto = true

src/lib.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ mod digest_trait09 {
223223
#[cfg(feature = "traits010")]
224224
pub type WrappedHash = digest010::core_api::CoreWrapper<Hash>;
225225

226+
#[cfg(feature = "traits011")]
227+
digest011::buffer_fixed!(
228+
/// Wrapped `Hash` type for the `Digest` trait (digest 0.11).
229+
pub struct WrappedHash011(Hash);
230+
oid: "1.3.14.3.2.26";
231+
impl: Debug Clone Default Reset HashMarker BlockSizeUser OutputSizeUser Update FixedOutput FixedOutputReset;
232+
);
233+
226234
#[cfg(feature = "traits010")]
227235
mod digest_trait010 {
228236
use core::fmt;
@@ -292,6 +300,75 @@ mod digest_trait010 {
292300
}
293301
}
294302

303+
#[cfg(feature = "traits011")]
304+
mod digest_trait011 {
305+
use core::fmt;
306+
307+
use digest011::{
308+
block_buffer::Eager,
309+
const_oid::{AssociatedOid, ObjectIdentifier},
310+
consts::{U20, U64},
311+
core_api::{
312+
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, FixedOutputCore,
313+
OutputSizeUser, Reset, UpdateCore,
314+
},
315+
HashMarker,
316+
};
317+
318+
use super::Hash;
319+
320+
impl AssociatedOid for Hash {
321+
const OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.3.14.3.2.26");
322+
}
323+
324+
impl AlgorithmName for Hash {
325+
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
326+
f.write_str("Sha1")
327+
}
328+
}
329+
330+
impl HashMarker for Hash {}
331+
332+
impl BufferKindUser for Hash {
333+
type BufferKind = Eager;
334+
}
335+
336+
impl BlockSizeUser for Hash {
337+
type BlockSize = U64;
338+
}
339+
340+
impl OutputSizeUser for Hash {
341+
type OutputSize = U20;
342+
}
343+
344+
impl UpdateCore for Hash {
345+
#[inline]
346+
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
347+
for block in blocks {
348+
self._update(block)
349+
}
350+
}
351+
}
352+
353+
impl FixedOutputCore for Hash {
354+
fn finalize_fixed_core(
355+
&mut self,
356+
buffer: &mut Buffer<Self>,
357+
out: &mut digest011::Output<Self>,
358+
) {
359+
self._update(buffer.get_data());
360+
let h = self.finalize();
361+
out.copy_from_slice(&h);
362+
}
363+
}
364+
365+
impl Reset for Hash {
366+
fn reset(&mut self) {
367+
*self = Self::new()
368+
}
369+
}
370+
}
371+
295372
#[test]
296373
fn main() {
297374
let h = Hash::hash(b"");
@@ -355,3 +432,25 @@ fn main_traits() {
355432
]
356433
);
357434
}
435+
436+
#[cfg(feature = "traits011")]
437+
#[test]
438+
fn main_traits011() {
439+
use digest011::Digest;
440+
let mut h = WrappedHash011::new();
441+
Digest::update(&mut h, b"");
442+
assert_eq!(
443+
h.finalize().as_slice(),
444+
&[218, 57, 163, 238, 94, 107, 75, 13, 50, 85, 191, 239, 149, 96, 24, 144, 175, 216, 7, 9]
445+
);
446+
447+
let mut h = WrappedHash011::new();
448+
Digest::update(&mut h, b"test");
449+
assert_eq!(
450+
h.finalize().as_slice(),
451+
&[
452+
169, 74, 143, 229, 204, 177, 155, 166, 28, 76, 8, 115, 211, 145, 233, 135, 152, 47,
453+
187, 211
454+
]
455+
);
456+
}

0 commit comments

Comments
 (0)