murmurhash3.js 4.3 KB

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