mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 03:07:45 +08:00
34 lines
901 B
Go
34 lines
901 B
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package ghash
|
|
|
|
// AP implements the classic AP hash algorithm for 32 bits.
|
|
func AP(str []byte) uint32 {
|
|
var hash uint32 = 0
|
|
for i := 0; i < len(str); i++ {
|
|
if (i & 1) == 0 {
|
|
hash ^= (hash << 7) ^ uint32(str[i]) ^ (hash >> 3)
|
|
} else {
|
|
hash ^= ^((hash << 11) ^ uint32(str[i]) ^ (hash >> 5)) + 1
|
|
}
|
|
}
|
|
return hash
|
|
}
|
|
|
|
// AP64 implements the classic AP hash algorithm for 64 bits.
|
|
func AP64(str []byte) uint64 {
|
|
var hash uint64 = 0
|
|
for i := 0; i < len(str); i++ {
|
|
if (i & 1) == 0 {
|
|
hash ^= (hash << 7) ^ uint64(str[i]) ^ (hash >> 3)
|
|
} else {
|
|
hash ^= ^((hash << 11) ^ uint64(str[i]) ^ (hash >> 5)) + 1
|
|
}
|
|
}
|
|
return hash
|
|
}
|