UNPKG

4.4 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * Javascript code in this page
4 *
5 * Copyright 2018 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * Javascript code in this page
21 */
22"use strict";
23
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.MurmurHash3_64 = void 0;
28
29var _util = require("../shared/util");
30
31var MurmurHash3_64 = function MurmurHash3_64Closure(seed) {
32 var MASK_HIGH = 0xffff0000;
33 var MASK_LOW = 0xffff;
34
35 function MurmurHash3_64(seed) {
36 var SEED = 0xc3d2e1f0;
37 this.h1 = seed ? seed & 0xffffffff : SEED;
38 this.h2 = seed ? seed & 0xffffffff : SEED;
39 }
40
41 MurmurHash3_64.prototype = {
42 update: function MurmurHash3_64_update(input) {
43 var data, length;
44
45 if ((0, _util.isString)(input)) {
46 data = new Uint8Array(input.length * 2);
47 length = 0;
48
49 for (var i = 0, ii = input.length; i < ii; i++) {
50 var code = input.charCodeAt(i);
51
52 if (code <= 0xff) {
53 data[length++] = code;
54 } else {
55 data[length++] = code >>> 8;
56 data[length++] = code & 0xff;
57 }
58 }
59 } else if ((0, _util.isArrayBuffer)(input)) {
60 data = input;
61 length = data.byteLength;
62 } else {
63 throw new Error('Wrong data format in MurmurHash3_64_update. ' + 'Input must be a string or array.');
64 }
65
66 var blockCounts = length >> 2;
67 var tailLength = length - blockCounts * 4;
68 var dataUint32 = new Uint32Array(data.buffer, 0, blockCounts);
69 var k1 = 0;
70 var k2 = 0;
71 var h1 = this.h1;
72 var h2 = this.h2;
73 var C1 = 0xcc9e2d51;
74 var C2 = 0x1b873593;
75 var C1_LOW = C1 & MASK_LOW;
76 var C2_LOW = C2 & MASK_LOW;
77
78 for (var _i = 0; _i < blockCounts; _i++) {
79 if (_i & 1) {
80 k1 = dataUint32[_i];
81 k1 = k1 * C1 & MASK_HIGH | k1 * C1_LOW & MASK_LOW;
82 k1 = k1 << 15 | k1 >>> 17;
83 k1 = k1 * C2 & MASK_HIGH | k1 * C2_LOW & MASK_LOW;
84 h1 ^= k1;
85 h1 = h1 << 13 | h1 >>> 19;
86 h1 = h1 * 5 + 0xe6546b64;
87 } else {
88 k2 = dataUint32[_i];
89 k2 = k2 * C1 & MASK_HIGH | k2 * C1_LOW & MASK_LOW;
90 k2 = k2 << 15 | k2 >>> 17;
91 k2 = k2 * C2 & MASK_HIGH | k2 * C2_LOW & MASK_LOW;
92 h2 ^= k2;
93 h2 = h2 << 13 | h2 >>> 19;
94 h2 = h2 * 5 + 0xe6546b64;
95 }
96 }
97
98 k1 = 0;
99
100 switch (tailLength) {
101 case 3:
102 k1 ^= data[blockCounts * 4 + 2] << 16;
103
104 case 2:
105 k1 ^= data[blockCounts * 4 + 1] << 8;
106
107 case 1:
108 k1 ^= data[blockCounts * 4];
109 k1 = k1 * C1 & MASK_HIGH | k1 * C1_LOW & MASK_LOW;
110 k1 = k1 << 15 | k1 >>> 17;
111 k1 = k1 * C2 & MASK_HIGH | k1 * C2_LOW & MASK_LOW;
112
113 if (blockCounts & 1) {
114 h1 ^= k1;
115 } else {
116 h2 ^= k1;
117 }
118
119 }
120
121 this.h1 = h1;
122 this.h2 = h2;
123 return this;
124 },
125 hexdigest: function MurmurHash3_64_hexdigest() {
126 var h1 = this.h1;
127 var h2 = this.h2;
128 h1 ^= h2 >>> 1;
129 h1 = h1 * 0xed558ccd & MASK_HIGH | h1 * 0x8ccd & MASK_LOW;
130 h2 = h2 * 0xff51afd7 & MASK_HIGH | ((h2 << 16 | h1 >>> 16) * 0xafd7ed55 & MASK_HIGH) >>> 16;
131 h1 ^= h2 >>> 1;
132 h1 = h1 * 0x1a85ec53 & MASK_HIGH | h1 * 0xec53 & MASK_LOW;
133 h2 = h2 * 0xc4ceb9fe & MASK_HIGH | ((h2 << 16 | h1 >>> 16) * 0xb9fe1a85 & MASK_HIGH) >>> 16;
134 h1 ^= h2 >>> 1;
135
136 for (var i = 0, arr = [h1, h2], str = ''; i < arr.length; i++) {
137 var hex = (arr[i] >>> 0).toString(16);
138
139 while (hex.length < 8) {
140 hex = '0' + hex;
141 }
142
143 str += hex;
144 }
145
146 return str;
147 }
148 };
149 return MurmurHash3_64;
150}();
151
152exports.MurmurHash3_64 = MurmurHash3_64;
\No newline at end of file