utils.d.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
  2. export type TypedArray = Int8Array | Uint8ClampedArray | Uint8Array | Uint16Array | Int16Array | Uint32Array | Int32Array;
  3. export declare const u8: (arr: TypedArray) => Uint8Array;
  4. export declare const u32: (arr: TypedArray) => Uint32Array;
  5. export declare const createView: (arr: TypedArray) => DataView;
  6. export declare const rotr: (word: number, shift: number) => number;
  7. export declare const isLE: boolean;
  8. /**
  9. * @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
  10. */
  11. export declare function bytesToHex(bytes: Uint8Array): string;
  12. /**
  13. * @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
  14. */
  15. export declare function hexToBytes(hex: string): Uint8Array;
  16. export declare const nextTick: () => Promise<void>;
  17. export declare function asyncLoop(iters: number, tick: number, cb: (i: number) => void): Promise<void>;
  18. /**
  19. * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
  20. */
  21. export declare function utf8ToBytes(str: string): Uint8Array;
  22. export type Input = Uint8Array | string;
  23. /**
  24. * Normalizes (non-hex) string or Uint8Array to Uint8Array.
  25. * Warning: when Uint8Array is passed, it would NOT get copied.
  26. * Keep in mind for future mutable operations.
  27. */
  28. export declare function toBytes(data: Input): Uint8Array;
  29. /**
  30. * Copies several Uint8Arrays into one.
  31. */
  32. export declare function concatBytes(...arrays: Uint8Array[]): Uint8Array;
  33. export declare abstract class Hash<T extends Hash<T>> {
  34. abstract blockLen: number;
  35. abstract outputLen: number;
  36. abstract update(buf: Input): this;
  37. abstract digestInto(buf: Uint8Array): void;
  38. abstract digest(): Uint8Array;
  39. /**
  40. * Resets internal state. Makes Hash instance unusable.
  41. * Reset is impossible for keyed hashes if key is consumed into state. If digest is not consumed
  42. * by user, they will need to manually call `destroy()` when zeroing is necessary.
  43. */
  44. abstract destroy(): void;
  45. /**
  46. * Clones hash instance. Unsafe: doesn't check whether `to` is valid. Can be used as `clone()`
  47. * when no options are passed.
  48. * Reasons to use `_cloneInto` instead of clone: 1) performance 2) reuse instance => all internal
  49. * buffers are overwritten => causes buffer overwrite which is used for digest in some cases.
  50. * There are no guarantees for clean-up because it's impossible in JS.
  51. */
  52. abstract _cloneInto(to?: T): T;
  53. clone(): T;
  54. }
  55. /**
  56. * XOF: streaming API to read digest in chunks.
  57. * Same as 'squeeze' in keccak/k12 and 'seek' in blake3, but more generic name.
  58. * When hash used in XOF mode it is up to user to call '.destroy' afterwards, since we cannot
  59. * destroy state, next call can require more bytes.
  60. */
  61. export type HashXOF<T extends Hash<T>> = Hash<T> & {
  62. xof(bytes: number): Uint8Array;
  63. xofInto(buf: Uint8Array): Uint8Array;
  64. };
  65. type EmptyObj = {};
  66. export declare function checkOpts<T1 extends EmptyObj, T2 extends EmptyObj>(defaults: T1, opts?: T2): T1 & T2;
  67. export type CHash = ReturnType<typeof wrapConstructor>;
  68. export declare function wrapConstructor<T extends Hash<T>>(hashCons: () => Hash<T>): {
  69. (msg: Input): Uint8Array;
  70. outputLen: number;
  71. blockLen: number;
  72. create(): Hash<T>;
  73. };
  74. export declare function wrapConstructorWithOpts<H extends Hash<H>, T extends Object>(hashCons: (opts?: T) => Hash<H>): {
  75. (msg: Input, opts?: T): Uint8Array;
  76. outputLen: number;
  77. blockLen: number;
  78. create(opts: T): Hash<H>;
  79. };
  80. export declare function wrapXOFConstructorWithOpts<H extends HashXOF<H>, T extends Object>(hashCons: (opts?: T) => HashXOF<H>): {
  81. (msg: Input, opts?: T): Uint8Array;
  82. outputLen: number;
  83. blockLen: number;
  84. create(opts: T): HashXOF<H>;
  85. };
  86. /**
  87. * Secure PRNG. Uses `crypto.getRandomValues`, which defers to OS.
  88. */
  89. export declare function randomBytes(bytesLength?: number): Uint8Array;
  90. export {};