UNPKG

3.95 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * JavaScript code in this page
4 *
5 * Copyright 2022 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("./util.js");
30
31const SEED = 0xc3d2e1f0;
32const MASK_HIGH = 0xffff0000;
33const MASK_LOW = 0xffff;
34
35class MurmurHash3_64 {
36 constructor(seed) {
37 this.h1 = seed ? seed & 0xffffffff : SEED;
38 this.h2 = seed ? seed & 0xffffffff : SEED;
39 }
40
41 update(input) {
42 let data, length;
43
44 if (typeof input === "string") {
45 data = new Uint8Array(input.length * 2);
46 length = 0;
47
48 for (let i = 0, ii = input.length; i < ii; i++) {
49 const code = input.charCodeAt(i);
50
51 if (code <= 0xff) {
52 data[length++] = code;
53 } else {
54 data[length++] = code >>> 8;
55 data[length++] = code & 0xff;
56 }
57 }
58 } else if ((0, _util.isArrayBuffer)(input)) {
59 data = input.slice();
60 length = data.byteLength;
61 } else {
62 throw new Error("Wrong data format in MurmurHash3_64_update. " + "Input must be a string or array.");
63 }
64
65 const blockCounts = length >> 2;
66 const tailLength = length - blockCounts * 4;
67 const dataUint32 = new Uint32Array(data.buffer, 0, blockCounts);
68 let k1 = 0,
69 k2 = 0;
70 let h1 = this.h1,
71 h2 = this.h2;
72 const C1 = 0xcc9e2d51,
73 C2 = 0x1b873593;
74 const C1_LOW = C1 & MASK_LOW,
75 C2_LOW = C2 & MASK_LOW;
76
77 for (let i = 0; i < blockCounts; i++) {
78 if (i & 1) {
79 k1 = dataUint32[i];
80 k1 = k1 * C1 & MASK_HIGH | k1 * C1_LOW & MASK_LOW;
81 k1 = k1 << 15 | k1 >>> 17;
82 k1 = k1 * C2 & MASK_HIGH | k1 * C2_LOW & MASK_LOW;
83 h1 ^= k1;
84 h1 = h1 << 13 | h1 >>> 19;
85 h1 = h1 * 5 + 0xe6546b64;
86 } else {
87 k2 = dataUint32[i];
88 k2 = k2 * C1 & MASK_HIGH | k2 * C1_LOW & MASK_LOW;
89 k2 = k2 << 15 | k2 >>> 17;
90 k2 = k2 * C2 & MASK_HIGH | k2 * C2_LOW & MASK_LOW;
91 h2 ^= k2;
92 h2 = h2 << 13 | h2 >>> 19;
93 h2 = h2 * 5 + 0xe6546b64;
94 }
95 }
96
97 k1 = 0;
98
99 switch (tailLength) {
100 case 3:
101 k1 ^= data[blockCounts * 4 + 2] << 16;
102
103 case 2:
104 k1 ^= data[blockCounts * 4 + 1] << 8;
105
106 case 1:
107 k1 ^= data[blockCounts * 4];
108 k1 = k1 * C1 & MASK_HIGH | k1 * C1_LOW & MASK_LOW;
109 k1 = k1 << 15 | k1 >>> 17;
110 k1 = k1 * C2 & MASK_HIGH | k1 * C2_LOW & MASK_LOW;
111
112 if (blockCounts & 1) {
113 h1 ^= k1;
114 } else {
115 h2 ^= k1;
116 }
117
118 }
119
120 this.h1 = h1;
121 this.h2 = h2;
122 }
123
124 hexdigest() {
125 let h1 = this.h1,
126 h2 = this.h2;
127 h1 ^= h2 >>> 1;
128 h1 = h1 * 0xed558ccd & MASK_HIGH | h1 * 0x8ccd & MASK_LOW;
129 h2 = h2 * 0xff51afd7 & MASK_HIGH | ((h2 << 16 | h1 >>> 16) * 0xafd7ed55 & MASK_HIGH) >>> 16;
130 h1 ^= h2 >>> 1;
131 h1 = h1 * 0x1a85ec53 & MASK_HIGH | h1 * 0xec53 & MASK_LOW;
132 h2 = h2 * 0xc4ceb9fe & MASK_HIGH | ((h2 << 16 | h1 >>> 16) * 0xb9fe1a85 & MASK_HIGH) >>> 16;
133 h1 ^= h2 >>> 1;
134 const hex1 = (h1 >>> 0).toString(16),
135 hex2 = (h2 >>> 0).toString(16);
136 return hex1.padStart(8, "0") + hex2.padStart(8, "0");
137 }
138
139}
140
141exports.MurmurHash3_64 = MurmurHash3_64;
\No newline at end of file