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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const FNV_OFFSET_BASIS_32: u32 = 0x811c9dc5;
#[allow(dead_code)]
const FNV_OFFSET_BASIS_64: u64 = 0xcbf29ce484222325;
const FNV_PRIME_32: u32 = 0x01000193;
#[allow(dead_code)]
const FNV_PRIME_64: u64 = 0x00000100000001B3;
pub const fn fnv1a_hash_64(bytes: &[u8], limit: Option<usize>) -> u64 {
let mut hash = FNV_OFFSET_BASIS_64;
let mut i = 0;
let len = match limit {
Some(v) => {
if v <= bytes.len() && v > 0 {
v
} else {
bytes.len()
}
}
None => bytes.len(),
};
while i < len {
hash ^= bytes[i] as u64;
hash = hash.wrapping_mul(FNV_PRIME_64);
i += 1;
}
hash
}
pub const fn fnv1a_hash_32(bytes: &[u8], limit: Option<usize>) -> u32 {
let mut hash = FNV_OFFSET_BASIS_32;
let mut i = 0;
let len = match limit {
Some(v) => {
if v <= bytes.len() && v > 0 {
v
} else {
bytes.len()
}
}
None => bytes.len(),
};
while i < len {
hash ^= bytes[i] as u32;
hash = hash.wrapping_mul(FNV_PRIME_32);
i += 1;
}
hash
}
#[allow(dead_code)]
pub const fn fnv1a_hash_16_xor(bytes: &[u8], limit: Option<usize>) -> u16 {
let bytes = fnv1a_hash_32(bytes, limit).to_ne_bytes();
let upper: u16 = u16::from_ne_bytes([bytes[0], bytes[1]]);
let lower: u16 = u16::from_ne_bytes([bytes[2], bytes[3]]);
upper ^ lower
}
#[allow(dead_code)]
pub const fn fnv1a_hash_str_64(input: &str) -> u64 {
fnv1a_hash_64(input.as_bytes(), None)
}
#[allow(dead_code)]
pub const fn fnv1a_hash_str_32(input: &str) -> u32 {
fnv1a_hash_32(input.as_bytes(), None)
}
#[allow(dead_code)]
pub const fn fnv1a_hash_str_16_xor(input: &str) -> u16 {
fnv1a_hash_16_xor(input.as_bytes(), None)
}