2
0

murmurhash3.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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");
  28. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  29. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  30. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  31. var SEED = 0xc3d2e1f0;
  32. var MASK_HIGH = 0xffff0000;
  33. var MASK_LOW = 0xffff;
  34. var MurmurHash3_64 =
  35. /*#__PURE__*/
  36. function () {
  37. function MurmurHash3_64(seed) {
  38. _classCallCheck(this, MurmurHash3_64);
  39. this.h1 = seed ? seed & 0xffffffff : SEED;
  40. this.h2 = seed ? seed & 0xffffffff : SEED;
  41. }
  42. _createClass(MurmurHash3_64, [{
  43. key: "update",
  44. value: function update(input) {
  45. var data, length;
  46. if ((0, _util.isString)(input)) {
  47. data = new Uint8Array(input.length * 2);
  48. length = 0;
  49. for (var i = 0, ii = input.length; i < ii; i++) {
  50. var code = input.charCodeAt(i);
  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;
  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. var blockCounts = length >> 2;
  65. var tailLength = length - blockCounts * 4;
  66. var dataUint32 = new Uint32Array(data.buffer, 0, blockCounts);
  67. var k1 = 0,
  68. k2 = 0;
  69. var h1 = this.h1,
  70. h2 = this.h2;
  71. var C1 = 0xcc9e2d51,
  72. C2 = 0x1b873593;
  73. var C1_LOW = C1 & MASK_LOW,
  74. C2_LOW = C2 & MASK_LOW;
  75. for (var _i = 0; _i < blockCounts; _i++) {
  76. if (_i & 1) {
  77. k1 = dataUint32[_i];
  78. k1 = k1 * C1 & MASK_HIGH | k1 * C1_LOW & MASK_LOW;
  79. k1 = k1 << 15 | k1 >>> 17;
  80. k1 = k1 * C2 & MASK_HIGH | k1 * C2_LOW & MASK_LOW;
  81. h1 ^= k1;
  82. h1 = h1 << 13 | h1 >>> 19;
  83. h1 = h1 * 5 + 0xe6546b64;
  84. } else {
  85. k2 = dataUint32[_i];
  86. k2 = k2 * C1 & MASK_HIGH | k2 * C1_LOW & MASK_LOW;
  87. k2 = k2 << 15 | k2 >>> 17;
  88. k2 = k2 * C2 & MASK_HIGH | k2 * C2_LOW & MASK_LOW;
  89. h2 ^= k2;
  90. h2 = h2 << 13 | h2 >>> 19;
  91. h2 = h2 * 5 + 0xe6546b64;
  92. }
  93. }
  94. k1 = 0;
  95. switch (tailLength) {
  96. case 3:
  97. k1 ^= data[blockCounts * 4 + 2] << 16;
  98. case 2:
  99. k1 ^= data[blockCounts * 4 + 1] << 8;
  100. case 1:
  101. k1 ^= data[blockCounts * 4];
  102. k1 = k1 * C1 & MASK_HIGH | k1 * C1_LOW & MASK_LOW;
  103. k1 = k1 << 15 | k1 >>> 17;
  104. k1 = k1 * C2 & MASK_HIGH | k1 * C2_LOW & MASK_LOW;
  105. if (blockCounts & 1) {
  106. h1 ^= k1;
  107. } else {
  108. h2 ^= k1;
  109. }
  110. }
  111. this.h1 = h1;
  112. this.h2 = h2;
  113. }
  114. }, {
  115. key: "hexdigest",
  116. value: function hexdigest() {
  117. var h1 = this.h1,
  118. h2 = this.h2;
  119. h1 ^= h2 >>> 1;
  120. h1 = h1 * 0xed558ccd & MASK_HIGH | h1 * 0x8ccd & MASK_LOW;
  121. h2 = h2 * 0xff51afd7 & MASK_HIGH | ((h2 << 16 | h1 >>> 16) * 0xafd7ed55 & MASK_HIGH) >>> 16;
  122. h1 ^= h2 >>> 1;
  123. h1 = h1 * 0x1a85ec53 & MASK_HIGH | h1 * 0xec53 & MASK_LOW;
  124. h2 = h2 * 0xc4ceb9fe & MASK_HIGH | ((h2 << 16 | h1 >>> 16) * 0xb9fe1a85 & MASK_HIGH) >>> 16;
  125. h1 ^= h2 >>> 1;
  126. var hex1 = (h1 >>> 0).toString(16),
  127. hex2 = (h2 >>> 0).toString(16);
  128. return hex1.padStart(8, '0') + hex2.padStart(8, '0');
  129. }
  130. }]);
  131. return MurmurHash3_64;
  132. }();
  133. exports.MurmurHash3_64 = MurmurHash3_64;