hash-instance.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { BaseHash as BaseHasher } from '../base/index.js';
  2. import { normalizeInput } from './hash-fn.js';
  3. import { mustGetEncoder } from './encoding.js';
  4. import { BrowserHashReader } from './hash-reader.js';
  5. import { Hash } from './hash.js';
  6. import { getWasm } from './wasm.js';
  7. /**
  8. * @inheritdoc
  9. */
  10. export class BrowserHasher extends BaseHasher {
  11. /**
  12. * @inheritdoc
  13. * @override
  14. */
  15. update(data) {
  16. return super.update(normalizeInput(data));
  17. }
  18. digest(encoding, options) {
  19. let resolvedOpts;
  20. let resolvedEnc;
  21. if (encoding && typeof encoding === 'object') {
  22. resolvedOpts = encoding;
  23. resolvedEnc = undefined;
  24. }
  25. else {
  26. resolvedOpts = options;
  27. resolvedEnc = encoding;
  28. }
  29. const result = super.digest(resolvedOpts);
  30. return resolvedEnc ? mustGetEncoder(resolvedEnc)(result) : result;
  31. }
  32. }
  33. /**
  34. * A Node.js crypto-like createHash method.
  35. */
  36. export const createHash = () => new BrowserHasher(getWasm().create_hasher(), l => new Hash(l), r => new BrowserHashReader(r));
  37. /**
  38. * A Node.js crypto-like createHash method.
  39. */
  40. export const createKeyed = (key) => new BrowserHasher(getWasm().create_keyed(key), l => new Hash(l), r => new BrowserHashReader(r));
  41. //# sourceMappingURL=hash-instance.js.map