noncrypto_digests/
xxh64.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
pub use ::xxhash_rust::xxh64::Xxh64 as Xxh64Hasher;
use digest::typenum::U8;
use digest::{FixedOutput, HashMarker, Output, OutputSizeUser, Update};

use crate::common::{impl_hash_wrapper, HashWrapper};

#[derive(Clone)]
pub struct Xxh64(Xxh64Hasher);

impl_hash_wrapper!(Xxh64, Xxh64Hasher, U8);

impl Default for Xxh64 {
    fn default() -> Self {
        Self(Xxh64Hasher::new(0))
    }
}

impl Update for Xxh64 {
    fn update(&mut self, data: &[u8]) {
        self.0.update(data);
    }
}

impl FixedOutput for Xxh64 {
    fn finalize_into(self, out: &mut Output<Self>) {
        let result = self.0.digest();
        let bytes = result.to_be_bytes();
        out.copy_from_slice(&bytes);
    }
}

#[cfg(test)]
mod tests {
    use insta::assert_snapshot;
    use xxhash_rust::xxh64::xxh64;

    use super::*;
    use crate::tests::hash;

    #[test]
    fn test_xxh64() {
        let default = xxh64(&[], 0);
        assert_eq!(hash::<Xxh64>(""), format!("{default:0>8X}"));
        assert_snapshot!(hash::<Xxh64>(""), @"EF46DB3751D8E999");
        assert_snapshot!(hash::<Xxh64>("hello"), @"26C7827D889F6DA3");
    }
}