murmurhash3.js 3.9 KB

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