murmurhash3.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. var sharedUtil = require('../shared/util.js');
  17. var Uint32ArrayView = sharedUtil.Uint32ArrayView;
  18. var MurmurHash3_64 = function MurmurHash3_64Closure(seed) {
  19. var MASK_HIGH = 0xffff0000;
  20. var MASK_LOW = 0xffff;
  21. function MurmurHash3_64(seed) {
  22. var SEED = 0xc3d2e1f0;
  23. this.h1 = seed ? seed & 0xffffffff : SEED;
  24. this.h2 = seed ? seed & 0xffffffff : SEED;
  25. }
  26. var alwaysUseUint32ArrayView = false;
  27. try {
  28. new Uint32Array(new Uint8Array(5).buffer, 0, 1);
  29. } catch (e) {
  30. alwaysUseUint32ArrayView = true;
  31. }
  32. MurmurHash3_64.prototype = {
  33. update: function MurmurHash3_64_update(input) {
  34. var useUint32ArrayView = alwaysUseUint32ArrayView;
  35. var i;
  36. if (typeof input === 'string') {
  37. var data = new Uint8Array(input.length * 2);
  38. var length = 0;
  39. for (i = 0; i < input.length; i++) {
  40. var code = input.charCodeAt(i);
  41. if (code <= 0xff) {
  42. data[length++] = code;
  43. } else {
  44. data[length++] = code >>> 8;
  45. data[length++] = code & 0xff;
  46. }
  47. }
  48. } else if (input instanceof Uint8Array) {
  49. data = input;
  50. length = data.length;
  51. } else if (typeof input === 'object' && 'length' in input) {
  52. data = input;
  53. length = data.length;
  54. useUint32ArrayView = true;
  55. } else {
  56. throw new Error('Wrong data format in MurmurHash3_64_update. ' + 'Input must be a string or array.');
  57. }
  58. var blockCounts = length >> 2;
  59. var tailLength = length - blockCounts * 4;
  60. var dataUint32 = useUint32ArrayView ? new Uint32ArrayView(data, blockCounts) : new Uint32Array(data.buffer, 0, blockCounts);
  61. var k1 = 0;
  62. var k2 = 0;
  63. var h1 = this.h1;
  64. var h2 = this.h2;
  65. var C1 = 0xcc9e2d51;
  66. var C2 = 0x1b873593;
  67. var C1_LOW = C1 & MASK_LOW;
  68. var C2_LOW = C2 & MASK_LOW;
  69. for (i = 0; i < blockCounts; i++) {
  70. if (i & 1) {
  71. k1 = dataUint32[i];
  72. k1 = k1 * C1 & MASK_HIGH | k1 * C1_LOW & MASK_LOW;
  73. k1 = k1 << 15 | k1 >>> 17;
  74. k1 = k1 * C2 & MASK_HIGH | k1 * C2_LOW & MASK_LOW;
  75. h1 ^= k1;
  76. h1 = h1 << 13 | h1 >>> 19;
  77. h1 = h1 * 5 + 0xe6546b64;
  78. } else {
  79. k2 = dataUint32[i];
  80. k2 = k2 * C1 & MASK_HIGH | k2 * C1_LOW & MASK_LOW;
  81. k2 = k2 << 15 | k2 >>> 17;
  82. k2 = k2 * C2 & MASK_HIGH | k2 * C2_LOW & MASK_LOW;
  83. h2 ^= k2;
  84. h2 = h2 << 13 | h2 >>> 19;
  85. h2 = h2 * 5 + 0xe6546b64;
  86. }
  87. }
  88. k1 = 0;
  89. switch (tailLength) {
  90. case 3:
  91. k1 ^= data[blockCounts * 4 + 2] << 16;
  92. case 2:
  93. k1 ^= data[blockCounts * 4 + 1] << 8;
  94. case 1:
  95. k1 ^= data[blockCounts * 4];
  96. k1 = k1 * C1 & MASK_HIGH | k1 * C1_LOW & MASK_LOW;
  97. k1 = k1 << 15 | k1 >>> 17;
  98. k1 = k1 * C2 & MASK_HIGH | k1 * C2_LOW & MASK_LOW;
  99. if (blockCounts & 1) {
  100. h1 ^= k1;
  101. } else {
  102. h2 ^= k1;
  103. }
  104. }
  105. this.h1 = h1;
  106. this.h2 = h2;
  107. return this;
  108. },
  109. hexdigest: function MurmurHash3_64_hexdigest() {
  110. var h1 = this.h1;
  111. var h2 = this.h2;
  112. h1 ^= h2 >>> 1;
  113. h1 = h1 * 0xed558ccd & MASK_HIGH | h1 * 0x8ccd & MASK_LOW;
  114. h2 = h2 * 0xff51afd7 & MASK_HIGH | ((h2 << 16 | h1 >>> 16) * 0xafd7ed55 & MASK_HIGH) >>> 16;
  115. h1 ^= h2 >>> 1;
  116. h1 = h1 * 0x1a85ec53 & MASK_HIGH | h1 * 0xec53 & MASK_LOW;
  117. h2 = h2 * 0xc4ceb9fe & MASK_HIGH | ((h2 << 16 | h1 >>> 16) * 0xb9fe1a85 & MASK_HIGH) >>> 16;
  118. h1 ^= h2 >>> 1;
  119. for (var i = 0, arr = [
  120. h1,
  121. h2
  122. ], str = ''; i < arr.length; i++) {
  123. var hex = (arr[i] >>> 0).toString(16);
  124. while (hex.length < 8) {
  125. hex = '0' + hex;
  126. }
  127. str += hex;
  128. }
  129. return str;
  130. }
  131. };
  132. return MurmurHash3_64;
  133. }();
  134. exports.MurmurHash3_64 = MurmurHash3_64;