2020-01-29 01:43:09 +03:00
|
|
|
module fnv1a
|
|
|
|
|
|
|
|
const (
|
|
|
|
fnv64_prime = 1099511628211
|
|
|
|
fnv64_offset_basis = 14695981039346656037
|
|
|
|
fnv32_offset_basis = u32(2166136261)
|
|
|
|
fnv32_prime = u32(16777619)
|
|
|
|
)
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn sum32_string(data string) u32 {
|
|
|
|
mut hash := fnv32_offset_basis
|
2020-02-24 19:55:16 +03:00
|
|
|
for i in 0..data.len {
|
2020-01-29 01:43:09 +03:00
|
|
|
hash = (hash ^ u32(data[i])) * fnv32_prime
|
|
|
|
}
|
|
|
|
return hash
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn sum32(data []byte) u32 {
|
|
|
|
mut hash := fnv32_offset_basis
|
2020-02-24 19:55:16 +03:00
|
|
|
for i in 0..data.len {
|
2020-01-29 01:43:09 +03:00
|
|
|
hash = (hash ^ u32(data[i])) * fnv32_prime
|
|
|
|
}
|
|
|
|
return hash
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn sum64_string(data string) u64 {
|
|
|
|
mut hash := fnv64_offset_basis
|
2020-02-24 19:55:16 +03:00
|
|
|
for i in 0..data.len {
|
2020-01-29 01:43:09 +03:00
|
|
|
hash = (hash ^ u64(data[i])) * fnv64_prime
|
|
|
|
}
|
|
|
|
return hash
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn sum64(data []byte) u64 {
|
|
|
|
mut hash := fnv64_offset_basis
|
2020-02-24 19:55:16 +03:00
|
|
|
for i in 0..data.len {
|
2020-01-29 01:43:09 +03:00
|
|
|
hash = (hash ^ u64(data[i])) * fnv64_prime
|
|
|
|
}
|
|
|
|
return hash
|
|
|
|
}
|