createHash.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Hash = require("./Hash");
  7. const BULK_SIZE = 2000;
  8. // We are using an object instead of a Map as this will stay static during the runtime
  9. // so access to it can be optimized by v8
  10. /** @type {{[key: string]: Map<string, string>}} */
  11. const digestCaches = {};
  12. /** @typedef {function(): Hash} HashFactory */
  13. class BulkUpdateDecorator extends Hash {
  14. /**
  15. * @param {Hash | HashFactory} hashOrFactory function to create a hash
  16. * @param {string=} hashKey key for caching
  17. */
  18. constructor(hashOrFactory, hashKey) {
  19. super();
  20. this.hashKey = hashKey;
  21. if (typeof hashOrFactory === "function") {
  22. this.hashFactory = hashOrFactory;
  23. this.hash = undefined;
  24. } else {
  25. this.hashFactory = undefined;
  26. this.hash = hashOrFactory;
  27. }
  28. this.buffer = "";
  29. }
  30. /**
  31. * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
  32. * @param {string|Buffer} data data
  33. * @param {string=} inputEncoding data encoding
  34. * @returns {this} updated hash
  35. */
  36. update(data, inputEncoding) {
  37. if (
  38. inputEncoding !== undefined ||
  39. typeof data !== "string" ||
  40. data.length > BULK_SIZE
  41. ) {
  42. if (this.hash === undefined)
  43. this.hash = /** @type {HashFactory} */ (this.hashFactory)();
  44. if (this.buffer.length > 0) {
  45. this.hash.update(this.buffer);
  46. this.buffer = "";
  47. }
  48. this.hash.update(data, inputEncoding);
  49. } else {
  50. this.buffer += data;
  51. if (this.buffer.length > BULK_SIZE) {
  52. if (this.hash === undefined)
  53. this.hash = /** @type {HashFactory} */ (this.hashFactory)();
  54. this.hash.update(this.buffer);
  55. this.buffer = "";
  56. }
  57. }
  58. return this;
  59. }
  60. /**
  61. * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
  62. * @param {string=} encoding encoding of the return value
  63. * @returns {string|Buffer} digest
  64. */
  65. digest(encoding) {
  66. let digestCache;
  67. const buffer = this.buffer;
  68. if (this.hash === undefined) {
  69. // short data for hash, we can use caching
  70. const cacheKey = `${this.hashKey}-${encoding}`;
  71. digestCache = digestCaches[cacheKey];
  72. if (digestCache === undefined) {
  73. digestCache = digestCaches[cacheKey] = new Map();
  74. }
  75. const cacheEntry = digestCache.get(buffer);
  76. if (cacheEntry !== undefined) return cacheEntry;
  77. this.hash = /** @type {HashFactory} */ (this.hashFactory)();
  78. }
  79. if (buffer.length > 0) {
  80. this.hash.update(buffer);
  81. }
  82. const digestResult = this.hash.digest(encoding);
  83. const result =
  84. typeof digestResult === "string" ? digestResult : digestResult.toString();
  85. if (digestCache !== undefined) {
  86. digestCache.set(buffer, result);
  87. }
  88. return result;
  89. }
  90. }
  91. /* istanbul ignore next */
  92. class DebugHash extends Hash {
  93. constructor() {
  94. super();
  95. this.string = "";
  96. }
  97. /**
  98. * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
  99. * @param {string|Buffer} data data
  100. * @param {string=} inputEncoding data encoding
  101. * @returns {this} updated hash
  102. */
  103. update(data, inputEncoding) {
  104. if (typeof data !== "string") data = data.toString("utf-8");
  105. const prefix = Buffer.from("@webpack-debug-digest@").toString("hex");
  106. if (data.startsWith(prefix)) {
  107. data = Buffer.from(data.slice(prefix.length), "hex").toString();
  108. }
  109. this.string += `[${data}](${
  110. /** @type {string} */ (new Error().stack).split("\n", 3)[2]
  111. })\n`;
  112. return this;
  113. }
  114. /**
  115. * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
  116. * @param {string=} encoding encoding of the return value
  117. * @returns {string|Buffer} digest
  118. */
  119. digest(encoding) {
  120. return Buffer.from("@webpack-debug-digest@" + this.string).toString("hex");
  121. }
  122. }
  123. /** @type {typeof import("crypto") | undefined} */
  124. let crypto = undefined;
  125. /** @type {typeof import("./hash/xxhash64") | undefined} */
  126. let createXXHash64 = undefined;
  127. /** @type {typeof import("./hash/md4") | undefined} */
  128. let createMd4 = undefined;
  129. /** @type {typeof import("./hash/BatchedHash") | undefined} */
  130. let BatchedHash = undefined;
  131. /**
  132. * Creates a hash by name or function
  133. * @param {string | typeof Hash} algorithm the algorithm name or a constructor creating a hash
  134. * @returns {Hash} the hash
  135. */
  136. module.exports = algorithm => {
  137. if (typeof algorithm === "function") {
  138. return new BulkUpdateDecorator(() => new algorithm());
  139. }
  140. switch (algorithm) {
  141. // TODO add non-cryptographic algorithm here
  142. case "debug":
  143. return new DebugHash();
  144. case "xxhash64":
  145. if (createXXHash64 === undefined) {
  146. createXXHash64 = require("./hash/xxhash64");
  147. if (BatchedHash === undefined) {
  148. BatchedHash = require("./hash/BatchedHash");
  149. }
  150. }
  151. return new /** @type {typeof import("./hash/BatchedHash")} */ (
  152. BatchedHash
  153. )(createXXHash64());
  154. case "md4":
  155. if (createMd4 === undefined) {
  156. createMd4 = require("./hash/md4");
  157. if (BatchedHash === undefined) {
  158. BatchedHash = require("./hash/BatchedHash");
  159. }
  160. }
  161. return new /** @type {typeof import("./hash/BatchedHash")} */ (
  162. BatchedHash
  163. )(createMd4());
  164. case "native-md4":
  165. if (crypto === undefined) crypto = require("crypto");
  166. return new BulkUpdateDecorator(
  167. () => /** @type {typeof import("crypto")} */ (crypto).createHash("md4"),
  168. "md4"
  169. );
  170. default:
  171. if (crypto === undefined) crypto = require("crypto");
  172. return new BulkUpdateDecorator(
  173. () =>
  174. /** @type {typeof import("crypto")} */ (crypto).createHash(algorithm),
  175. algorithm
  176. );
  177. }
  178. };