numberHash.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /**
  7. * The maximum safe integer value for 32-bit integers.
  8. * @type {number}
  9. */
  10. const SAFE_LIMIT = 0x80000000;
  11. /**
  12. * The maximum safe integer value for 32-bit integers minus one. This is used
  13. * in the algorithm to ensure that intermediate hash values do not exceed the
  14. * 32-bit integer limit.
  15. * @type {number}
  16. */
  17. const SAFE_PART = SAFE_LIMIT - 1;
  18. /**
  19. * The number of 32-bit integers used to store intermediate hash values.
  20. * @type {number}
  21. */
  22. const COUNT = 4;
  23. /**
  24. * An array used to store intermediate hash values during the calculation.
  25. * @type {number[]}
  26. */
  27. const arr = [0, 0, 0, 0, 0];
  28. /**
  29. * An array of prime numbers used in the hash calculation.
  30. * @type {number[]}
  31. */
  32. const primes = [3, 7, 17, 19];
  33. /**
  34. * Computes a hash value for the given string and range. This hashing algorithm is a modified
  35. * version of the [FNV-1a algorithm](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function).
  36. * It is optimized for speed and does **not** generate a cryptographic hash value.
  37. *
  38. * We use `numberHash` in `lib/ids/IdHelpers.js` to generate hash values for the module identifier. The generated
  39. * hash is used as a prefix for the module id's to avoid collisions with other modules.
  40. *
  41. * @param {string} str The input string to hash.
  42. * @param {number} range The range of the hash value (0 to range-1).
  43. * @returns {number} - The computed hash value.
  44. *
  45. * @example
  46. *
  47. * ```js
  48. * const numberHash = require("webpack/lib/util/numberHash");
  49. * numberHash("hello", 1000); // 57
  50. * numberHash("hello world"); // 990
  51. * ```
  52. *
  53. */
  54. module.exports = (str, range) => {
  55. /**
  56. * Initialize the array with zeros before it is used
  57. * to store intermediate hash values.
  58. */
  59. arr.fill(0);
  60. // For each character in the string
  61. for (let i = 0; i < str.length; i++) {
  62. // Get the character code.
  63. const c = str.charCodeAt(i);
  64. // For each 32-bit integer used to store the hash value
  65. // add the character code to the current hash value and multiply by the prime number and
  66. // add the previous 32-bit integer.
  67. arr[0] = (arr[0] + c * primes[0] + arr[3]) & SAFE_PART;
  68. arr[1] = (arr[1] + c * primes[1] + arr[0]) & SAFE_PART;
  69. arr[2] = (arr[2] + c * primes[2] + arr[1]) & SAFE_PART;
  70. arr[3] = (arr[3] + c * primes[3] + arr[2]) & SAFE_PART;
  71. // For each 32-bit integer used to store the hash value
  72. // XOR the current hash value with the value of the next 32-bit integer.
  73. arr[0] = arr[0] ^ (arr[arr[0] % COUNT] >> 1);
  74. arr[1] = arr[1] ^ (arr[arr[1] % COUNT] >> 1);
  75. arr[2] = arr[2] ^ (arr[arr[2] % COUNT] >> 1);
  76. arr[3] = arr[3] ^ (arr[arr[3] % COUNT] >> 1);
  77. }
  78. if (range <= SAFE_PART) {
  79. return (arr[0] + arr[1] + arr[2] + arr[3]) % range;
  80. } else {
  81. // Calculate the range extension.
  82. const rangeExt = Math.floor(range / SAFE_LIMIT);
  83. const sum1 = (arr[0] + arr[2]) & SAFE_PART;
  84. const sum2 = (arr[0] + arr[2]) % rangeExt;
  85. return (sum2 * SAFE_LIMIT + sum1) % range;
  86. }
  87. };