noncrypto_digests/
xxh64.rs

1pub use ::xxhash_rust::xxh64::Xxh64 as Xxh64Hasher;
2use digest::typenum::U8;
3use digest::{FixedOutput, HashMarker, Output, OutputSizeUser, Update};
4
5use crate::common::{impl_hash_wrapper, HashWrapper};
6
7#[derive(Clone)]
8pub struct Xxh64(Xxh64Hasher);
9
10impl_hash_wrapper!(Xxh64, Xxh64Hasher, U8);
11
12impl Default for Xxh64 {
13    fn default() -> Self {
14        Self(Xxh64Hasher::new(0))
15    }
16}
17
18impl Update for Xxh64 {
19    fn update(&mut self, data: &[u8]) {
20        self.0.update(data);
21    }
22}
23
24impl FixedOutput for Xxh64 {
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::xxh64::xxh64;
36
37    use super::*;
38    use crate::tests::hash;
39
40    #[test]
41    fn test_xxh64() {
42        let default = xxh64(&[], 0);
43        assert_eq!(hash::<Xxh64>(""), format!("{default:0>8X}"));
44        assert_snapshot!(hash::<Xxh64>(""), @"EF46DB3751D8E999");
45        assert_snapshot!(hash::<Xxh64>("hello"), @"26C7827D889F6DA3");
46    }
47}