hash-instance.d.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { BaseHashInput, IBaseHashOptions } from './hash-fn';
  2. import { IHashReader } from './hash-reader';
  3. /**
  4. * A blake3 hash. Quite similar to Node's crypto hashing.
  5. *
  6. * Note that you must call {@link IHash#dispose} or {@link IHash#done} when
  7. * you're finished with it to free memory.
  8. */
  9. export interface IHasher<T> {
  10. /**
  11. * Adds the given data to the hash.
  12. * @throws {Error} if {@link IHash#digest} has already been called.
  13. */
  14. update(data: BaseHashInput): this;
  15. /**
  16. * Returns a digest of the hash.
  17. *
  18. * If `dispose: false` is given in the options, the hash will not
  19. * automatically be disposed of, allowing you to continue updating
  20. * it after obtaining the current reader.
  21. */
  22. digest(options?: IBaseHashOptions & {
  23. dispose?: boolean;
  24. }): T;
  25. /**
  26. * Returns a {@link HashReader} for the current hash.
  27. *
  28. * If `dispose: false` is given in the options, the hash will not
  29. * automatically be disposed of, allowing you to continue updating
  30. * it after obtaining the current reader.
  31. */
  32. reader(options?: {
  33. dispose?: boolean;
  34. }): IHashReader<T>;
  35. /**
  36. * Frees data associated with the hash. This *must* be called if
  37. * {@link IHash#digest} is not called in order to free memory.
  38. */
  39. dispose(): void;
  40. }
  41. /**
  42. * @hidden
  43. */
  44. export interface IInternalHash<Reader> {
  45. free(): void;
  46. reader(): Reader;
  47. update(bytes: Uint8Array): void;
  48. digest(into: Uint8Array): void;
  49. }
  50. export interface IHasherDigestOptions extends IBaseHashOptions {
  51. dispose?: boolean;
  52. }
  53. /**
  54. * Base implementation of hashing.
  55. */
  56. export declare class BaseHash<Binary extends Uint8Array, InternalReader, Reader extends IHashReader<Binary>> implements IHasher<Binary> {
  57. private readonly alloc;
  58. private readonly getReader;
  59. private hash;
  60. constructor(implementation: IInternalHash<InternalReader>, alloc: (length: number) => Binary, getReader: (internal: InternalReader) => Reader);
  61. /**
  62. * @inheritdoc
  63. */
  64. update(data: BaseHashInput): this;
  65. /**
  66. * @inheritdoc
  67. */
  68. digest({ length, dispose }?: IHasherDigestOptions): Binary;
  69. /**
  70. * @inheritdoc
  71. */
  72. reader({ dispose }?: {
  73. dispose?: boolean;
  74. }): Reader;
  75. /**
  76. * @inheritdoc
  77. */
  78. dispose(): void;
  79. }