noncrypto_digests/
xxh32.rs

1pub use ::xxhash_rust::xxh32::Xxh32 as Xxh32Hasher;
2use digest::typenum::U4;
3use digest::{FixedOutput, HashMarker, Output, OutputSizeUser, Update};
4
5use crate::common::{impl_hash_wrapper, HashWrapper};
6
7#[derive(Clone)]
8pub struct Xxh32(Xxh32Hasher);
9
10impl_hash_wrapper!(Xxh32, Xxh32Hasher, U4);
11
12impl Default for Xxh32 {
13    fn default() -> Self {
14        Self(Xxh32Hasher::new(0))
15    }
16}
17
18impl Update for Xxh32 {
19    fn update(&mut self, data: &[u8]) {
20        self.0.update(data);
21    }
22}
23
24impl FixedOutput for Xxh32 {
25    fn finalize_into(self, out: &mut Output<Self>) {
26        let result = self.0.digest();
27        let bytes = result.to_be_bytes();
28        out.copy_from_slice(&bytes);
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use insta::assert_snapshot;
35    use xxhash_rust::xxh32::xxh32;
36
37    use super::*;
38    use crate::tests::hash;
39
40    #[test]
41    fn test_xxh32() {
42        let default = xxh32(&[], 0);
43        assert_eq!(hash::<Xxh32>(""), format!("{default:0>8X}"));
44        assert_snapshot!(hash::<Xxh32>(""), @"02CC5D05");
45        assert_snapshot!(hash::<Xxh32>("hello"), @"FB0077F9");
46    }
47}