-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ghash_elf.go
39 lines (36 loc) · 879 Bytes
/
ghash_elf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 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 {
var (
hash uint32
x uint32
)
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 (
hash uint64
x uint64
)
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
}