-
Notifications
You must be signed in to change notification settings - Fork 13
/
jenkinshash.go
103 lines (90 loc) · 2.11 KB
/
jenkinshash.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package gocrush
const (
MAX_VALUE = int64(0xFFFFFFFF)
CRUSH_HASH_SEED = int64(1315423911)
)
type Triple struct {
a int64
b int64
c int64
}
func hash1(a int64) int64 {
var hash = xor(CRUSH_HASH_SEED, a)
var x = int64(231232)
var y = int64(1232)
b := a
b, x, hash = hashMix(b, x, hash)
y, a, hash = hashMix(y, a, hash)
return hash
}
func hash2(a, b int64) int64 {
var hash = xor(xor(CRUSH_HASH_SEED, a), b)
var x = int64(231232)
var y = int64(1232)
a, b, hash = hashMix(a, b, hash)
x, a, hash = hashMix(x, a, hash)
b, y, hash = hashMix(b, y, hash)
return hash
}
func hash3(a, b, c int64) int64 {
var hash = xor(xor(xor(CRUSH_HASH_SEED, a), b), c)
var x = int64(231232)
var y = int64(1232)
a, b, hash = hashMix(a, b, hash)
c, x, hash = hashMix(c, x, hash)
y, a, hash = hashMix(y, a, hash)
b, x, hash = hashMix(b, x, hash)
_, _, hash = hashMix(y, c, hash)
return hash
}
func hash4(a, b, c, d int64) int64 {
var hash = xor(xor(xor(xor(CRUSH_HASH_SEED, a), b), c), d)
var x = int64(231232)
var y = int64(1232)
a, b, hash = hashMix(a, b, hash)
c, x, hash = hashMix(c, d, hash)
c, x, hash = hashMix(a, x, hash)
y, a, hash = hashMix(y, b, hash)
b, x, hash = hashMix(c, x, hash)
_, _, hash = hashMix(y, d, hash)
return hash
}
func hashMix(a, b, c int64) (int64, int64, int64) {
a = subtract(a, b)
a = subtract(a, c)
a = xor(a, c>>13)
b = subtract(b, c)
b = subtract(b, a)
b = xor(b, leftShift(a, 8))
c = subtract(c, a)
c = subtract(c, b)
c = xor(c, (b >> 13))
a = subtract(a, b)
a = subtract(a, c)
a = xor(a, (c >> 12))
b = subtract(b, c)
b = subtract(b, a)
b = xor(b, leftShift(a, 16))
c = subtract(c, a)
c = subtract(c, b)
c = xor(c, (b >> 5))
a = subtract(a, b)
a = subtract(a, c)
a = xor(a, (c >> 3))
b = subtract(b, c)
b = subtract(b, a)
b = xor(b, leftShift(a, 10))
c = subtract(c, a)
c = subtract(c, b)
c = xor(c, (b >> 15))
return a, b, c
}
func subtract(val, subtract int64) int64 {
return (val - subtract) & MAX_VALUE
}
func leftShift(val, shift int64) int64 {
return (val << uint64(shift)) & MAX_VALUE
}
func xor(val, xor int64) int64 {
return (val ^ xor) & MAX_VALUE
}