createHash.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {{new(): Hash}} HashConstructor */
  7. /**
  8. * @typedef {Object} Hash
  9. * @property {function(string|Buffer, string=): Hash} update
  10. * @property {function(string): string} digest
  11. */
  12. const BULK_SIZE = 1000;
  13. /**
  14. * @implements {Hash}
  15. */
  16. class BulkUpdateDecorator {
  17. constructor(hash) {
  18. this.hash = hash;
  19. this.buffer = "";
  20. }
  21. update(data, inputEncoding) {
  22. if (
  23. inputEncoding !== undefined ||
  24. typeof data !== "string" ||
  25. data.length > BULK_SIZE
  26. ) {
  27. if (this.buffer.length > 0) {
  28. this.hash.update(this.buffer);
  29. this.buffer = "";
  30. }
  31. this.hash.update(data, inputEncoding);
  32. } else {
  33. this.buffer += data;
  34. if (this.buffer.length > BULK_SIZE) {
  35. this.hash.update(this.buffer);
  36. this.buffer = "";
  37. }
  38. }
  39. return this;
  40. }
  41. digest(encoding) {
  42. if (this.buffer.length > 0) {
  43. this.hash.update(this.buffer);
  44. }
  45. var digestResult = this.hash.digest(encoding);
  46. return typeof digestResult === "string"
  47. ? digestResult
  48. : digestResult.toString();
  49. }
  50. }
  51. /* istanbul ignore next */
  52. class DebugHash {
  53. constructor() {
  54. this.string = "";
  55. }
  56. update(data, inputEncoding) {
  57. if (typeof data !== "string") data = data.toString("utf-8");
  58. this.string += data;
  59. return this;
  60. }
  61. digest(encoding) {
  62. return this.string.replace(/[^a-z0-9]+/gi, m =>
  63. Buffer.from(m).toString("hex")
  64. );
  65. }
  66. }
  67. /**
  68. * Creates a hash by name or function
  69. * @param {string | HashConstructor} algorithm the algorithm name or a constructor creating a hash
  70. * @returns {Hash} the hash
  71. */
  72. module.exports = algorithm => {
  73. if (typeof algorithm === "function") {
  74. return new BulkUpdateDecorator(new algorithm());
  75. }
  76. switch (algorithm) {
  77. // TODO add non-cryptographic algorithm here
  78. case "debug":
  79. return new DebugHash();
  80. default:
  81. return new BulkUpdateDecorator(require("crypto").createHash(algorithm));
  82. }
  83. };