hash-instance.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const hash_fn_1 = require("./hash-fn");
  4. const index_1 = require("../base/index");
  5. const stream_1 = require("stream");
  6. const wasm_1 = require("./wasm");
  7. const hash_reader_1 = require("./hash-reader");
  8. /**
  9. * @inheritdoc
  10. */
  11. class NodeHash extends stream_1.Transform {
  12. constructor(implementation, getReader) {
  13. super();
  14. this.hash = new index_1.BaseHash(implementation, l => Buffer.alloc(l), getReader);
  15. }
  16. /**
  17. * @reader
  18. */
  19. reader(options) {
  20. const reader = this.hash.reader(options);
  21. return reader;
  22. }
  23. /**
  24. * @inheritdoc
  25. */
  26. update(data, encoding) {
  27. this.hash.update(hash_fn_1.normalizeInput(data, encoding));
  28. return this;
  29. }
  30. digest(encoding, options) {
  31. let resolvedOpts;
  32. let resolvedEnc;
  33. if (encoding && typeof encoding === 'object') {
  34. resolvedOpts = encoding;
  35. resolvedEnc = undefined;
  36. }
  37. else {
  38. resolvedOpts = options;
  39. resolvedEnc = encoding;
  40. }
  41. const result = this.hash.digest(resolvedOpts);
  42. return resolvedEnc ? result.toString(resolvedEnc) : result;
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. dispose() {
  48. this.hash.dispose();
  49. }
  50. /**
  51. * @inheritdoc
  52. * @hidden
  53. */
  54. _transform(chunk, encoding, callback) {
  55. this.update(chunk, encoding);
  56. callback();
  57. }
  58. /**
  59. * @inheritdoc
  60. * @hidden
  61. */
  62. _flush(callback) {
  63. callback(null, this.digest());
  64. }
  65. }
  66. exports.NodeHash = NodeHash;
  67. /**
  68. * A Node.js crypto-like createHash method.
  69. */
  70. exports.createHash = () => new NodeHash(wasm_1.getWasm().create_hasher(), r => new hash_reader_1.NodeHashReader(r));
  71. /**
  72. * Construct a new Hasher for the keyed hash function.
  73. */
  74. exports.createKeyed = (key) => new NodeHash(wasm_1.getWasm().create_keyed(key), r => new hash_reader_1.NodeHashReader(r));
  75. /**
  76. * Construct a new Hasher for the key derivation function.
  77. */
  78. exports.createDeriveKey = (context) => new NodeHash(wasm_1.getWasm().create_derive(context), r => new hash_reader_1.NodeHashReader(r));
  79. //# sourceMappingURL=hash-instance.js.map