utils.d.ts 4.2 KB

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