Skip to main content

md5_simd/
state.rs

1//! Copyable multi-stream lane state (shares [`crate::core::Raw`] with [`crate::Md5`]).
2
3use crate::backend;
4use crate::core::Raw;
5
6/// Incremental state for one MD5 message in a multi-stream workload.
7///
8/// `finalize` is a **snapshot**: the state is not modified and may receive
9/// more data afterwards.
10#[derive(Clone, Copy, Debug)]
11pub struct Md5State {
12    raw: Raw,
13}
14
15impl Default for Md5State {
16    #[inline]
17    fn default() -> Self {
18        Self::new()
19    }
20}
21
22impl Md5State {
23    /// Empty lane state.
24    #[inline]
25    pub const fn new() -> Self {
26        Self { raw: Raw::new() }
27    }
28
29    /// Append bytes to this lane.
30    #[inline]
31    pub fn update(&mut self, input: &[u8]) {
32        self.raw.update_opt(input);
33    }
34
35    /// Snapshot digest; state is left unchanged.
36    #[inline]
37    #[must_use]
38    pub fn finalize(&self) -> [u8; 16] {
39        self.raw.digest_snapshot(backend::compress_block)
40    }
41
42    /// Reset to empty.
43    #[inline]
44    pub fn reset(&mut self) {
45        self.raw.reset();
46    }
47
48    /// Bytes absorbed so far.
49    #[inline]
50    pub const fn bytes_hashed(&self) -> u64 {
51        self.raw.count
52    }
53
54    /// Whether nothing has been absorbed since construction/reset.
55    #[inline]
56    pub const fn is_empty(&self) -> bool {
57        self.raw.count == 0
58    }
59}