sha512.d.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. declare type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE";
  2. declare type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY";
  3. declare type FormatType = "TEXT" | FormatNoTextType;
  4. declare type GenericInputType = {
  5. value: string;
  6. format: "TEXT";
  7. encoding?: EncodingType;
  8. } | {
  9. value: string;
  10. format: "B64" | "HEX" | "BYTES";
  11. } | {
  12. value: ArrayBuffer;
  13. format: "ARRAYBUFFER";
  14. } | {
  15. value: Uint8Array;
  16. format: "UINT8ARRAY";
  17. };
  18. declare type FixedLengthOptionsNoEncodingType = {
  19. hmacKey?: GenericInputType;
  20. } | {
  21. numRounds?: number;
  22. };
  23. declare type FixedLengthOptionsEncodingType = {
  24. hmacKey?: GenericInputType;
  25. encoding?: EncodingType;
  26. } | {
  27. numRounds?: number;
  28. encoding?: EncodingType;
  29. };
  30. interface packedValue {
  31. value: number[];
  32. binLen: number;
  33. }
  34. declare abstract class jsSHABase<StateT, VariantT> {
  35. /**
  36. * @param variant The desired SHA variant.
  37. * @param inputFormat The input format to be used in future `update` calls.
  38. * @param options Hashmap of extra input options.
  39. */
  40. protected readonly shaVariant: VariantT;
  41. protected readonly inputFormat: FormatType;
  42. protected readonly utfType: EncodingType;
  43. protected readonly numRounds: number;
  44. protected abstract intermediateState: StateT;
  45. protected keyWithIPad: number[];
  46. protected keyWithOPad: number[];
  47. protected remainder: number[];
  48. protected remainderLen: number;
  49. protected updateCalled: boolean;
  50. protected processedLen: number;
  51. protected macKeySet: boolean;
  52. protected abstract readonly variantBlockSize: number;
  53. protected abstract readonly bigEndianMod: -1 | 1;
  54. protected abstract readonly outputBinLen: number;
  55. protected abstract readonly isVariableLen: boolean;
  56. protected abstract readonly HMACSupported: boolean;
  57. protected abstract readonly converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue;
  58. protected abstract readonly roundFunc: (block: number[], H: StateT) => StateT;
  59. protected abstract readonly finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: StateT, outputLen: number) => number[];
  60. protected abstract readonly stateCloneFunc: (state: StateT) => StateT;
  61. protected abstract readonly newStateFunc: (variant: VariantT) => StateT;
  62. protected abstract readonly getMAC: ((options: {
  63. outputLen: number;
  64. }) => number[]) | null;
  65. protected constructor(variant: VariantT, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType);
  66. protected constructor(variant: VariantT, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType);
  67. /**
  68. * Hashes as many blocks as possible. Stores the rest for either a future update or getHash call.
  69. *
  70. * @param srcString The input to be hashed.
  71. */
  72. update(srcString: string | ArrayBuffer | Uint8Array): void;
  73. /**
  74. * Returns the desired SHA hash of the input fed in via `update` calls.
  75. *
  76. * @param format The desired output formatting
  77. * @param options Hashmap of output formatting options. `outputLen` must be specified for variable length hashes.
  78. * `outputLen` replaces the now deprecated `shakeLen` key.
  79. * @returns The hash in the format specified.
  80. */
  81. getHash(format: "HEX", options?: {
  82. outputUpper?: boolean;
  83. outputLen?: number;
  84. shakeLen?: number;
  85. }): string;
  86. getHash(format: "B64", options?: {
  87. b64Pad?: string;
  88. outputLen?: number;
  89. shakeLen?: number;
  90. }): string;
  91. getHash(format: "BYTES", options?: {
  92. outputLen?: number;
  93. shakeLen?: number;
  94. }): string;
  95. getHash(format: "UINT8ARRAY", options?: {
  96. outputLen?: number;
  97. shakeLen?: number;
  98. }): Uint8Array;
  99. getHash(format: "ARRAYBUFFER", options?: {
  100. outputLen?: number;
  101. shakeLen?: number;
  102. }): ArrayBuffer;
  103. /**
  104. * Sets the HMAC key for an eventual `getHMAC` call. Must be called immediately after jsSHA object instantiation.
  105. *
  106. * @param key The key used to calculate the HMAC
  107. * @param inputFormat The format of key.
  108. * @param options Hashmap of extra input options.
  109. */
  110. setHMACKey(key: string, inputFormat: "TEXT", options?: {
  111. encoding?: EncodingType;
  112. }): void;
  113. setHMACKey(key: string, inputFormat: "B64" | "HEX" | "BYTES"): void;
  114. setHMACKey(key: ArrayBuffer, inputFormat: "ARRAYBUFFER"): void;
  115. setHMACKey(key: Uint8Array, inputFormat: "UINT8ARRAY"): void;
  116. /**
  117. * Internal function that sets the MAC key.
  118. *
  119. * @param key The packed MAC key to use
  120. */
  121. protected _setHMACKey(key: packedValue): void;
  122. /**
  123. * Returns the the HMAC in the specified format using the key given by a previous `setHMACKey` call.
  124. *
  125. * @param format The desired output formatting.
  126. * @param options Hashmap of extra outputs options.
  127. * @returns The HMAC in the format specified.
  128. */
  129. getHMAC(format: "HEX", options?: {
  130. outputUpper?: boolean;
  131. }): string;
  132. getHMAC(format: "B64", options?: {
  133. b64Pad?: string;
  134. }): string;
  135. getHMAC(format: "BYTES"): string;
  136. getHMAC(format: "UINT8ARRAY"): Uint8Array;
  137. getHMAC(format: "ARRAYBUFFER"): ArrayBuffer;
  138. /**
  139. * Internal function that returns the "raw" HMAC
  140. */
  141. protected _getHMAC(): number[];
  142. }
  143. /**
  144. * Int_64 is a object for 2 32-bit numbers emulating a 64-bit number.
  145. */
  146. declare class Int_64 {
  147. /**
  148. * @param msint_32 The most significant 32-bits of a 64-bit number.
  149. * @param lsint_32 The least significant 32-bits of a 64-bit number.
  150. */
  151. readonly highOrder: number;
  152. readonly lowOrder: number;
  153. constructor(msint_32: number, lsint_32: number);
  154. }
  155. declare type VariantType = "SHA-384" | "SHA-512";
  156. declare class jsSHA extends jsSHABase<Int_64[], VariantType> {
  157. intermediateState: Int_64[];
  158. variantBlockSize: number;
  159. bigEndianMod: -1 | 1;
  160. outputBinLen: number;
  161. isVariableLen: boolean;
  162. HMACSupported: boolean;
  163. converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue;
  164. roundFunc: (block: number[], H: Int_64[]) => Int_64[];
  165. finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: Int_64[]) => number[];
  166. stateCloneFunc: (state: Int_64[]) => Int_64[];
  167. newStateFunc: (variant: VariantType) => Int_64[];
  168. getMAC: () => number[];
  169. constructor(variant: VariantType, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType);
  170. constructor(variant: VariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType);
  171. }
  172. export default jsSHA;