Skip to main content

md5_simd/
digest_compat.rs

1//! RustCrypto `digest` 0.11 compatibility (feature = `"digest"`).
2//!
3//! Wraps the shared [`crate::core::Raw`] state machine so Digest call sites
4//! use the same compressor/padding as [`crate::Md5`].
5
6use core::fmt;
7
8use crate::backend;
9use crate::core::Raw;
10use digest::block_api::{
11    AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, Eager, FixedOutputCore,
12    OutputSizeUser, Reset, UpdateCore,
13};
14use digest::typenum::{U16, U64};
15use digest::{HashMarker, Output};
16
17/// Core MD5 state for the Digest trait wrapper.
18#[derive(Clone)]
19pub struct Md5Core {
20    raw: Raw,
21}
22
23impl Default for Md5Core {
24    #[inline]
25    fn default() -> Self {
26        Self { raw: Raw::new() }
27    }
28}
29
30impl HashMarker for Md5Core {}
31
32impl BlockSizeUser for Md5Core {
33    type BlockSize = U64;
34}
35
36impl BufferKindUser for Md5Core {
37    type BufferKind = Eager;
38}
39
40impl OutputSizeUser for Md5Core {
41    type OutputSize = U16;
42}
43
44impl UpdateCore for Md5Core {
45    #[inline]
46    fn update_blocks(&mut self, blocks: &[Block<Self>]) {
47        debug_assert_eq!(self.raw.buf_len, 0);
48        let data = Block::<Self>::slice_as_flattened(blocks);
49        backend::compress_blocks(&mut self.raw.state, data);
50        self.raw.count = self.raw.count.wrapping_add(data.len() as u64);
51    }
52}
53
54impl FixedOutputCore for Md5Core {
55    #[inline]
56    fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
57        let tail = buffer.get_data();
58        let digest = crate::frame::finalize_with(
59            self.raw.state,
60            self.raw.count.wrapping_add(tail.len() as u64),
61            tail,
62            backend::compress_block,
63        );
64        out.copy_from_slice(&digest);
65    }
66}
67
68impl Reset for Md5Core {
69    #[inline]
70    fn reset(&mut self) {
71        self.raw.reset();
72    }
73}
74
75impl AlgorithmName for Md5Core {
76    fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
77        f.write_str("Md5")
78    }
79}
80
81impl fmt::Debug for Md5Core {
82    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83        f.write_str("Md5Core { .. }")
84    }
85}
86
87digest::buffer_fixed!(
88    /// Streaming MD5 hasher compatible with RustCrypto `Digest` (0.11).
89    pub struct Md5(Md5Core);
90    impl: BaseFixedTraits AlgorithmName Default Clone HashMarker Reset FixedOutputReset;
91);