hash_index/
lib.rs

1use blake3;
2
3pub fn hash(input: &[u8]) -> u32 {
4  hash_with_max(input, std::u32::MAX)
5}
6
7pub fn hash_with_max(input: &[u8], max: u32) -> u32 {
8  let hash = blake3::hash(input);
9  let hash = hash.as_bytes().to_vec();
10
11  let be16_bytes = u16::from_be_bytes([hash[0], hash[1]]);
12  let be32_bytes = u32::from_be_bytes([hash[0], hash[1], hash[2], hash[3]]);
13
14  let num = u32::from(be16_bytes)
15    .overflowing_mul(0xffffffff)
16    .0
17    .overflowing_add(be32_bytes)
18    .0;
19
20  num % max
21}