hash-instance.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const hash_fn_1 = require("./hash-fn");
  4. /**
  5. * Base implementation of hashing.
  6. */
  7. class BaseHash {
  8. constructor(implementation, alloc, getReader) {
  9. this.alloc = alloc;
  10. this.getReader = getReader;
  11. this.hash = implementation;
  12. }
  13. /**
  14. * @inheritdoc
  15. */
  16. update(data) {
  17. if (!this.hash) {
  18. throw new Error('Cannot continue updating hashing after dispose() has been called');
  19. }
  20. this.hash.update(hash_fn_1.inputToArray(data));
  21. return this;
  22. }
  23. /**
  24. * @inheritdoc
  25. */
  26. digest({ length = hash_fn_1.defaultHashLength, dispose = true } = {}) {
  27. if (!this.hash) {
  28. throw new Error('Cannot call digest() after dipose() has been called');
  29. }
  30. const digested = this.alloc(length);
  31. this.hash.digest(digested);
  32. if (dispose) {
  33. this.dispose();
  34. }
  35. return digested;
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. reader({ dispose = true } = {}) {
  41. if (!this.hash) {
  42. throw new Error('Cannot call reader() after dipose() has been called');
  43. }
  44. const reader = this.getReader(this.hash.reader());
  45. if (dispose) {
  46. this.dispose();
  47. }
  48. return reader;
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. dispose() {
  54. var _a;
  55. (_a = this.hash) === null || _a === void 0 ? void 0 : _a.free();
  56. this.hash = undefined;
  57. }
  58. }
  59. exports.BaseHash = BaseHash;
  60. //# sourceMappingURL=hash-instance.js.map