2020-01-29 01:43:09 +03:00
|
|
|
|
module fnv1a
|
|
|
|
|
|
2021-12-29 12:27:26 +03:00
|
|
|
|
// This module implements a FNV-1a hash.
|
|
|
|
|
// (see https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function)
|
|
|
|
|
|
2020-01-29 01:43:09 +03:00
|
|
|
|
const (
|
2021-05-08 13:32:29 +03:00
|
|
|
|
fnv64_prime = u64(1099511628211)
|
2020-03-23 22:05:37 +03:00
|
|
|
|
fnv64_offset_basis = u64(14695981039346656037)
|
2020-01-29 01:43:09 +03:00
|
|
|
|
fnv32_offset_basis = u32(2166136261)
|
2021-05-08 13:32:29 +03:00
|
|
|
|
fnv32_prime = u32(16777619)
|
2020-01-29 01:43:09 +03:00
|
|
|
|
)
|
|
|
|
|
|
2021-12-29 12:27:26 +03:00
|
|
|
|
// sum32 returns a fnv1a hash of the string, described by `data`
|
|
|
|
|
[direct_array_access; inline]
|
2020-01-29 01:43:09 +03:00
|
|
|
|
pub fn sum32_string(data string) u32 {
|
2021-05-08 13:32:29 +03:00
|
|
|
|
mut hash := fnv1a.fnv32_offset_basis
|
|
|
|
|
for i in 0 .. data.len {
|
|
|
|
|
hash = (hash ^ u32(data[i])) * fnv1a.fnv32_prime
|
2020-01-29 01:43:09 +03:00
|
|
|
|
}
|
|
|
|
|
return hash
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 12:27:26 +03:00
|
|
|
|
// sum32 returns a fnv1a hash of the memory block, described by the dynamic
|
|
|
|
|
// byte array `data`.
|
|
|
|
|
[direct_array_access; inline]
|
2022-04-15 15:35:35 +03:00
|
|
|
|
pub fn sum32(data []u8) u32 {
|
2021-05-08 13:32:29 +03:00
|
|
|
|
mut hash := fnv1a.fnv32_offset_basis
|
|
|
|
|
for i in 0 .. data.len {
|
|
|
|
|
hash = (hash ^ u32(data[i])) * fnv1a.fnv32_prime
|
2020-01-29 01:43:09 +03:00
|
|
|
|
}
|
|
|
|
|
return hash
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 13:03:24 +03:00
|
|
|
|
// sum32_bytes returns a fnv1a hash of the struct `s`.
|
|
|
|
|
[direct_array_access; inline]
|
|
|
|
|
pub fn sum32_struct<T>(s &T) u32 {
|
2022-04-15 14:58:56 +03:00
|
|
|
|
bp := unsafe { &u8(s) }
|
2021-12-29 13:03:24 +03:00
|
|
|
|
sz := int(sizeof(T))
|
|
|
|
|
mut hash := fnv1a.fnv32_offset_basis
|
|
|
|
|
for i in 0 .. sz {
|
|
|
|
|
hash = unsafe { (hash ^ u32(bp[i])) * fnv1a.fnv32_prime }
|
|
|
|
|
}
|
|
|
|
|
return hash
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 12:27:26 +03:00
|
|
|
|
// sum32_bytes returns a fnv1a hash of `data_len` bytes starting at
|
|
|
|
|
// the address in the given &byte pointer `data`.
|
|
|
|
|
[direct_array_access; inline; unsafe]
|
2022-04-15 18:25:45 +03:00
|
|
|
|
pub fn sum32_bytes(data &u8, data_len int) u32 {
|
2021-12-29 12:27:26 +03:00
|
|
|
|
mut hash := fnv1a.fnv32_offset_basis
|
|
|
|
|
for i in 0 .. data_len {
|
|
|
|
|
hash = unsafe { (hash ^ u32(data[i])) * fnv1a.fnv32_prime }
|
|
|
|
|
}
|
|
|
|
|
return hash
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sum64 returns a fnv1a hash of the string, described by `data`
|
|
|
|
|
[direct_array_access; inline]
|
2020-01-29 01:43:09 +03:00
|
|
|
|
pub fn sum64_string(data string) u64 {
|
2021-05-08 13:32:29 +03:00
|
|
|
|
mut hash := fnv1a.fnv64_offset_basis
|
|
|
|
|
for i in 0 .. data.len {
|
|
|
|
|
hash = (hash ^ u64(data[i])) * fnv1a.fnv64_prime
|
2020-01-29 01:43:09 +03:00
|
|
|
|
}
|
|
|
|
|
return hash
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 12:27:26 +03:00
|
|
|
|
// sum64 returns a fnv1a hash of the memory block, described by the dynamic
|
|
|
|
|
// byte array `data`.
|
|
|
|
|
[direct_array_access; inline]
|
2022-04-15 15:35:35 +03:00
|
|
|
|
pub fn sum64(data []u8) u64 {
|
2021-05-08 13:32:29 +03:00
|
|
|
|
mut hash := fnv1a.fnv64_offset_basis
|
|
|
|
|
for i in 0 .. data.len {
|
|
|
|
|
hash = (hash ^ u64(data[i])) * fnv1a.fnv64_prime
|
2020-01-29 01:43:09 +03:00
|
|
|
|
}
|
|
|
|
|
return hash
|
|
|
|
|
}
|
2021-12-29 12:27:26 +03:00
|
|
|
|
|
|
|
|
|
// sum64_bytes returns a fnv1a hash of `data_len` bytes starting at
|
|
|
|
|
// the address in the given &byte pointer `data`.
|
|
|
|
|
[direct_array_access; inline; unsafe]
|
2022-04-15 18:25:45 +03:00
|
|
|
|
pub fn sum64_bytes(data &u8, data_len int) u64 {
|
2021-12-29 12:27:26 +03:00
|
|
|
|
mut hash := fnv1a.fnv64_offset_basis
|
|
|
|
|
for i in 0 .. data_len {
|
|
|
|
|
hash = unsafe { (hash ^ u64(data[i])) * fnv1a.fnv64_prime }
|
|
|
|
|
}
|
|
|
|
|
return hash
|
|
|
|
|
}
|
2021-12-29 13:03:24 +03:00
|
|
|
|
|
|
|
|
|
// sum64_bytes returns a fnv1a hash of the struct `s`.
|
|
|
|
|
[direct_array_access; inline]
|
|
|
|
|
pub fn sum64_struct<T>(s &T) u64 {
|
2022-04-15 14:58:56 +03:00
|
|
|
|
bp := unsafe { &u8(s) }
|
2021-12-29 13:03:24 +03:00
|
|
|
|
sz := int(sizeof(T))
|
|
|
|
|
mut hash := fnv1a.fnv64_offset_basis
|
|
|
|
|
for i in 0 .. sz {
|
|
|
|
|
hash = unsafe { (hash ^ u64(bp[i])) * fnv1a.fnv64_prime }
|
|
|
|
|
}
|
|
|
|
|
return hash
|
|
|
|
|
}
|