2022-01-13 21:31:23 +08:00
|
|
|
// 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
|
|
|
|
|
|
|
|
// ELF implements the classic ELF hash algorithm for 32 bits.
|
|
|
|
func ELF(str []byte) uint32 {
|
2022-11-01 20:12:21 +08:00
|
|
|
var (
|
|
|
|
hash uint32
|
|
|
|
x uint32
|
|
|
|
)
|
2022-01-13 21:31:23 +08:00
|
|
|
for i := 0; i < len(str); i++ {
|
|
|
|
hash = (hash << 4) + uint32(str[i])
|
|
|
|
if x = hash & 0xF0000000; x != 0 {
|
|
|
|
hash ^= x >> 24
|
|
|
|
hash &= ^x + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hash
|
|
|
|
}
|
|
|
|
|
|
|
|
// ELF64 implements the classic ELF hash algorithm for 64 bits.
|
|
|
|
func ELF64(str []byte) uint64 {
|
|
|
|
var (
|
2022-11-01 20:12:21 +08:00
|
|
|
hash uint64
|
|
|
|
x uint64
|
2022-01-13 21:31:23 +08:00
|
|
|
)
|
|
|
|
for i := 0; i < len(str); i++ {
|
|
|
|
hash = (hash << 4) + uint64(str[i])
|
|
|
|
if x = hash & 0xF000000000000000; x != 0 {
|
|
|
|
hash ^= x >> 24
|
|
|
|
hash &= ^x + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hash
|
|
|
|
}
|