-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpengyhash.go
67 lines (48 loc) · 1.13 KB
/
pengyhash.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package pengyhash
import "encoding/binary"
func V64(p []byte) uint64 {
return binary.LittleEndian.Uint64(p)
}
func pengyhash(buf []byte, size int, seed uint64) uint64 {
p := buf
s := [4]uint64{0}
f := [4]uint64{0}
s[0] = uint64(size)
for size >= 32 {
s[1] += V64(p[8:])
s[0] += s[1] + V64(p[:])
s[1] = s[0] ^ (s[1]<<14 | s[1]>>50)
s[3] += V64(p[24:])
s[2] += s[3] + V64(p[16:])
s[3] = s[2] ^ (s[3]<<23 | s[3]>>41)
s[3] += V64(p[24:])
s[0] += s[3] + V64(p[:])
s[3] = s[0] ^ (s[3]<<11 | s[3]>>53)
s[1] += V64(p[8:])
s[2] += s[1] + V64(p[16:])
s[1] = s[2] ^ (s[1]<<40 | s[1]>>24)
size -= 32
p = p[32:]
}
for i := 0; i+8 <= size; i += 8 {
f[i/8] = V64(p[i:])
}
for i := 0; i < size; i++ {
f[i/8] |= uint64(p[i]) << (i % 8 * 8)
}
for i := 0; i < 6; i++ {
s[1] += f[1] + seed
s[0] += s[1] + f[0]
s[1] = s[0] ^ (s[1]<<14 | s[1]>>50)
s[3] += f[3]
s[2] += s[3] + f[2]
s[3] = s[2] ^ (s[3]<<23 | s[3]>>41)
s[3] += f[3]
s[0] += s[3] + f[0]
s[3] = s[0] ^ (s[3]<<9 | s[3]>>55)
s[1] += f[1]
s[2] += s[1] + f[2]
s[1] = s[2] ^ (s[1]<<40 | s[1]>>24)
}
return s[0] + s[1] + s[2] + s[3]
}