hash32/
fnv.rs

1use crate::Hasher as _;
2
3const BASIS: u32 = 0x811c9dc5;
4const PRIME: u32 = 0x1000193;
5
6/// 32-bit Fowler-Noll-Vo hasher
7pub struct Hasher {
8    state: u32,
9}
10
11impl Default for Hasher {
12    fn default() -> Self {
13        Hasher { state: BASIS }
14    }
15}
16
17impl crate::Hasher for Hasher {
18    #[inline]
19    fn finish32(&self) -> u32 {
20        self.state
21    }
22}
23
24impl core::hash::Hasher for Hasher {
25    #[inline]
26    fn write(&mut self, bytes: &[u8]) {
27        for byte in bytes {
28            self.state ^= u32::from(*byte);
29            self.state = self.state.wrapping_mul(PRIME);
30        }
31    }
32
33    #[inline]
34    fn finish(&self) -> u64 {
35        self.finish32().into()
36    }
37}