2020-01-29 01:43:09 +03:00
|
|
|
module fnv1a
|
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn sum32(data []byte) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn sum64(data []byte) 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
|
|
|
|
}
|