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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
// MurmurHash3 related functions
// https://github.com/markogresak/fingerprintjs2/blob/master/src/x64hash128.js
// Given two 64bit ints (as an array of two 32bit ints) returns the two
// added together as a 64bit int (as an array of two 32bit ints).
var x64Add = function (t, r) {
(t = [t[0] >>> 16, 65535 & t[0], t[1] >>> 16, 65535 & t[1]]),
(r = [r[0] >>> 16, 65535 & r[0], r[1] >>> 16, 65535 & r[1]]);
var e = [0, 0, 0, 0];
return (
(e[3] += t[3] + r[3]),
(e[2] += e[3] >>> 16),
(e[3] &= 65535),
(e[2] += t[2] + r[2]),
(e[1] += e[2] >>> 16),
(e[2] &= 65535),
(e[1] += t[1] + r[1]),
(e[0] += e[1] >>> 16),
(e[1] &= 65535),
(e[0] += t[0] + r[0]),
(e[0] &= 65535),
[(e[0] << 16) | e[1], (e[2] << 16) | e[3]]
);
},
// Given two 64bit ints (as an array of two 32bit ints) returns the two
// multiplied together as a 64bit int (as an array of two 32bit ints).
x64Multiply = function (t, r) {
(t = [t[0] >>> 16, 65535 & t[0], t[1] >>> 16, 65535 & t[1]]),
(r = [r[0] >>> 16, 65535 & r[0], r[1] >>> 16, 65535 & r[1]]);
var e = [0, 0, 0, 0];
return (
(e[3] += t[3] * r[3]),
(e[2] += e[3] >>> 16),
(e[3] &= 65535),
(e[2] += t[2] * r[3]),
(e[1] += e[2] >>> 16),
(e[2] &= 65535),
(e[2] += t[3] * r[2]),
(e[1] += e[2] >>> 16),
(e[2] &= 65535),
(e[1] += t[1] * r[3]),
(e[0] += e[1] >>> 16),
(e[1] &= 65535),
(e[1] += t[2] * r[2]),
(e[0] += e[1] >>> 16),
(e[1] &= 65535),
(e[1] += t[3] * r[1]),
(e[0] += e[1] >>> 16),
(e[1] &= 65535),
(e[0] += t[0] * r[3] + t[1] * r[2] + t[2] * r[1] + t[3] * r[0]),
(e[0] &= 65535),
[(e[0] << 16) | e[1], (e[2] << 16) | e[3]]
);
},
// Given a 64bit int (as an array of two 32bit ints) and an int
// representing a number of bit positions, returns the 64bit int (as an
// array of two 32bit ints) rotated left by that number of positions.
x64Rotl = function (t, r) {
return 32 === (r %= 64)
? [t[1], t[0]]
: r < 32
? [
(t[0] << r) | (t[1] >>> (32 - r)),
(t[1] << r) | (t[0] >>> (32 - r)),
]
: ((r -= 32),
[
(t[1] << r) | (t[0] >>> (32 - r)),
(t[0] << r) | (t[1] >>> (32 - r)),
]);
},
// Given a 64bit int (as an array of two 32bit ints) and an int
// representing a number of bit positions, returns the 64bit int (as an
// array of two 32bit ints) shifted left by that number of positions.
x64LeftShift = function (t, r) {
return 0 === (r %= 64)
? t
: r < 32
? [(t[0] << r) | (t[1] >>> (32 - r)), t[1] << r]
: [t[1] << (r - 32), 0];
},
// Given two 64bit ints (as an array of two 32bit ints) returns the two
// xored together as a 64bit int (as an array of two 32bit ints).
x64Xor = function (t, r) {
return [t[0] ^ r[0], t[1] ^ r[1]];
},
// Given a block, returns murmurHash3's final x64 mix of that block.
// (`[0, h[0] >>> 1]` is a 33 bit unsigned right shift. This is the
// only place where we need to right shift 64bit ints.)
x64Fmix = function (t) {
return (
(t = x64Xor(t, [0, t[0] >>> 1])),
(t = x64Multiply(t, [4283543511, 3981806797])),
(t = x64Xor(t, [0, t[0] >>> 1])),
(t = x64Multiply(t, [3301882366, 444984403])),
(t = x64Xor(t, [0, t[0] >>> 1]))
);
},
// Given a string and an optional seed as an int, returns a 128 bit
// hash using the x64 flavor of MurmurHash3, as an unsigned hex.
x64hash128 = function (t, r) {
r = r || 0;
for (
var e = (t = t || "").length % 16,
o = t.length - e,
x = [0, r],
c = [0, r],
h = [0, 0],
a = [0, 0],
d = [2277735313, 289559509],
i = [1291169091, 658871167],
l = 0;
l < o;
l += 16
)
(h = [
(255 & t.charCodeAt(l + 4)) |
((255 & t.charCodeAt(l + 5)) << 8) |
((255 & t.charCodeAt(l + 6)) << 16) |
((255 & t.charCodeAt(l + 7)) << 24),
(255 & t.charCodeAt(l)) |
((255 & t.charCodeAt(l + 1)) << 8) |
((255 & t.charCodeAt(l + 2)) << 16) |
((255 & t.charCodeAt(l + 3)) << 24),
]),
(a = [
(255 & t.charCodeAt(l + 12)) |
((255 & t.charCodeAt(l + 13)) << 8) |
((255 & t.charCodeAt(l + 14)) << 16) |
((255 & t.charCodeAt(l + 15)) << 24),
(255 & t.charCodeAt(l + 8)) |
((255 & t.charCodeAt(l + 9)) << 8) |
((255 & t.charCodeAt(l + 10)) << 16) |
((255 & t.charCodeAt(l + 11)) << 24),
]),
(h = x64Multiply(h, d)),
(h = x64Rotl(h, 31)),
(h = x64Multiply(h, i)),
(x = x64Xor(x, h)),
(x = x64Rotl(x, 27)),
(x = x64Add(x, c)),
(x = x64Add(x64Multiply(x, [0, 5]), [0, 1390208809])),
(a = x64Multiply(a, i)),
(a = x64Rotl(a, 33)),
(a = x64Multiply(a, d)),
(c = x64Xor(c, a)),
(c = x64Rotl(c, 31)),
(c = x64Add(c, x)),
(c = x64Add(x64Multiply(c, [0, 5]), [0, 944331445]));
switch (((h = [0, 0]), (a = [0, 0]), e)) {
case 15:
a = x64Xor(a, x64LeftShift([0, t.charCodeAt(l + 14)], 48));
case 14:
a = x64Xor(a, x64LeftShift([0, t.charCodeAt(l + 13)], 40));
case 13:
a = x64Xor(a, x64LeftShift([0, t.charCodeAt(l + 12)], 32));
case 12:
a = x64Xor(a, x64LeftShift([0, t.charCodeAt(l + 11)], 24));
case 11:
a = x64Xor(a, x64LeftShift([0, t.charCodeAt(l + 10)], 16));
case 10:
a = x64Xor(a, x64LeftShift([0, t.charCodeAt(l + 9)], 8));
case 9:
(a = x64Xor(a, [0, t.charCodeAt(l + 8)])),
(a = x64Multiply(a, i)),
(a = x64Rotl(a, 33)),
(a = x64Multiply(a, d)),
(c = x64Xor(c, a));
case 8:
h = x64Xor(h, x64LeftShift([0, t.charCodeAt(l + 7)], 56));
case 7:
h = x64Xor(h, x64LeftShift([0, t.charCodeAt(l + 6)], 48));
case 6:
h = x64Xor(h, x64LeftShift([0, t.charCodeAt(l + 5)], 40));
case 5:
h = x64Xor(h, x64LeftShift([0, t.charCodeAt(l + 4)], 32));
case 4:
h = x64Xor(h, x64LeftShift([0, t.charCodeAt(l + 3)], 24));
case 3:
h = x64Xor(h, x64LeftShift([0, t.charCodeAt(l + 2)], 16));
case 2:
h = x64Xor(h, x64LeftShift([0, t.charCodeAt(l + 1)], 8));
case 1:
(h = x64Xor(h, [0, t.charCodeAt(l)])),
(h = x64Multiply(h, d)),
(h = x64Rotl(h, 31)),
(h = x64Multiply(h, i)),
(x = x64Xor(x, h));
}
return (
(x = x64Xor(x, [0, t.length])),
(c = x64Xor(c, [0, t.length])),
(x = x64Add(x, c)),
(c = x64Add(c, x)),
(x = x64Fmix(x)),
(c = x64Fmix(c)),
(x = x64Add(x, c)),
(c = x64Add(c, x)),
("00000000" + (x[0] >>> 0).toString(16)).slice(-8) +
("00000000" + (x[1] >>> 0).toString(16)).slice(-8) +
("00000000" + (c[0] >>> 0).toString(16)).slice(-8) +
("00000000" + (c[1] >>> 0).toString(16)).slice(-8)
);
};
export default x64hash128;
|