getHashDigest.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. "use strict";
  2. const baseEncodeTables = {
  3. 26: "abcdefghijklmnopqrstuvwxyz",
  4. 32: "123456789abcdefghjkmnpqrstuvwxyz", // no 0lio
  5. 36: "0123456789abcdefghijklmnopqrstuvwxyz",
  6. 49: "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", // no lIO
  7. 52: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
  8. 58: "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", // no 0lIO
  9. 62: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
  10. 64: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_",
  11. };
  12. /**
  13. * @param {Uint32Array} uint32Array Treated as a long base-0x100000000 number, little endian
  14. * @param {number} divisor The divisor
  15. * @return {number} Modulo (remainder) of the division
  16. */
  17. function divmod32(uint32Array, divisor) {
  18. let carry = 0;
  19. for (let i = uint32Array.length - 1; i >= 0; i--) {
  20. const value = carry * 0x100000000 + uint32Array[i];
  21. carry = value % divisor;
  22. uint32Array[i] = Math.floor(value / divisor);
  23. }
  24. return carry;
  25. }
  26. function encodeBufferToBase(buffer, base, length) {
  27. const encodeTable = baseEncodeTables[base];
  28. if (!encodeTable) {
  29. throw new Error("Unknown encoding base" + base);
  30. }
  31. // Input bits are only enough to generate this many characters
  32. const limit = Math.ceil((buffer.length * 8) / Math.log2(base));
  33. length = Math.min(length, limit);
  34. // Most of the crypto digests (if not all) has length a multiple of 4 bytes.
  35. // Fewer numbers in the array means faster math.
  36. const uint32Array = new Uint32Array(Math.ceil(buffer.length / 4));
  37. // Make sure the input buffer data is copied and is not mutated by reference.
  38. // divmod32() would corrupt the BulkUpdateDecorator cache otherwise.
  39. buffer.copy(Buffer.from(uint32Array.buffer));
  40. let output = "";
  41. for (let i = 0; i < length; i++) {
  42. output = encodeTable[divmod32(uint32Array, base)] + output;
  43. }
  44. return output;
  45. }
  46. let crypto = undefined;
  47. let createXXHash64 = undefined;
  48. let createMd4 = undefined;
  49. let BatchedHash = undefined;
  50. let BulkUpdateDecorator = undefined;
  51. function getHashDigest(buffer, algorithm, digestType, maxLength) {
  52. algorithm = algorithm || "xxhash64";
  53. maxLength = maxLength || 9999;
  54. let hash;
  55. if (algorithm === "xxhash64") {
  56. if (createXXHash64 === undefined) {
  57. createXXHash64 = require("./hash/xxhash64");
  58. if (BatchedHash === undefined) {
  59. BatchedHash = require("./hash/BatchedHash");
  60. }
  61. }
  62. hash = new BatchedHash(createXXHash64());
  63. } else if (algorithm === "md4") {
  64. if (createMd4 === undefined) {
  65. createMd4 = require("./hash/md4");
  66. if (BatchedHash === undefined) {
  67. BatchedHash = require("./hash/BatchedHash");
  68. }
  69. }
  70. hash = new BatchedHash(createMd4());
  71. } else if (algorithm === "native-md4") {
  72. if (typeof crypto === "undefined") {
  73. crypto = require("crypto");
  74. if (BulkUpdateDecorator === undefined) {
  75. BulkUpdateDecorator = require("./hash/BulkUpdateDecorator");
  76. }
  77. }
  78. hash = new BulkUpdateDecorator(() => crypto.createHash("md4"), "md4");
  79. } else {
  80. if (typeof crypto === "undefined") {
  81. crypto = require("crypto");
  82. if (BulkUpdateDecorator === undefined) {
  83. BulkUpdateDecorator = require("./hash/BulkUpdateDecorator");
  84. }
  85. }
  86. hash = new BulkUpdateDecorator(
  87. () => crypto.createHash(algorithm),
  88. algorithm
  89. );
  90. }
  91. hash.update(buffer);
  92. if (
  93. digestType === "base26" ||
  94. digestType === "base32" ||
  95. digestType === "base36" ||
  96. digestType === "base49" ||
  97. digestType === "base52" ||
  98. digestType === "base58" ||
  99. digestType === "base62" ||
  100. digestType === "base64safe"
  101. ) {
  102. return encodeBufferToBase(
  103. hash.digest(),
  104. digestType === "base64safe" ? 64 : digestType.substr(4),
  105. maxLength
  106. );
  107. }
  108. return hash.digest(digestType || "hex").substr(0, maxLength);
  109. }
  110. module.exports = getHashDigest;