sha512.d.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE";
  2. type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY";
  3. type FormatType = "TEXT" | FormatNoTextType;
  4. 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. type FixedLengthOptionsNoEncodingType = {
  19. hmacKey?: GenericInputType;
  20. } | {
  21. numRounds?: number;
  22. };
  23. 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. * @returns A reference to the object.
  72. */
  73. update(srcString: string | ArrayBuffer | Uint8Array): this;
  74. /**
  75. * Returns the desired SHA hash of the input fed in via `update` calls.
  76. *
  77. * @param format The desired output formatting
  78. * @param options Hashmap of output formatting options. `outputLen` must be specified for variable length hashes.
  79. * `outputLen` replaces the now deprecated `shakeLen` key.
  80. * @returns The hash in the format specified.
  81. */
  82. getHash(format: "HEX", options?: {
  83. outputUpper?: boolean;
  84. outputLen?: number;
  85. shakeLen?: number;
  86. }): string;
  87. getHash(format: "B64", options?: {
  88. b64Pad?: string;
  89. outputLen?: number;
  90. shakeLen?: number;
  91. }): string;
  92. getHash(format: "BYTES", options?: {
  93. outputLen?: number;
  94. shakeLen?: number;
  95. }): string;
  96. getHash(format: "UINT8ARRAY", options?: {
  97. outputLen?: number;
  98. shakeLen?: number;
  99. }): Uint8Array;
  100. getHash(format: "ARRAYBUFFER", options?: {
  101. outputLen?: number;
  102. shakeLen?: number;
  103. }): ArrayBuffer;
  104. /**
  105. * Sets the HMAC key for an eventual `getHMAC` call. Must be called immediately after jsSHA object instantiation.
  106. *
  107. * @param key The key used to calculate the HMAC
  108. * @param inputFormat The format of key.
  109. * @param options Hashmap of extra input options.
  110. */
  111. setHMACKey(key: string, inputFormat: "TEXT", options?: {
  112. encoding?: EncodingType;
  113. }): void;
  114. setHMACKey(key: string, inputFormat: "B64" | "HEX" | "BYTES"): void;
  115. setHMACKey(key: ArrayBuffer, inputFormat: "ARRAYBUFFER"): void;
  116. setHMACKey(key: Uint8Array, inputFormat: "UINT8ARRAY"): void;
  117. /**
  118. * Internal function that sets the MAC key.
  119. *
  120. * @param key The packed MAC key to use
  121. */
  122. protected _setHMACKey(key: packedValue): void;
  123. /**
  124. * Returns the the HMAC in the specified format using the key given by a previous `setHMACKey` call.
  125. *
  126. * @param format The desired output formatting.
  127. * @param options Hashmap of extra outputs options.
  128. * @returns The HMAC in the format specified.
  129. */
  130. getHMAC(format: "HEX", options?: {
  131. outputUpper?: boolean;
  132. }): string;
  133. getHMAC(format: "B64", options?: {
  134. b64Pad?: string;
  135. }): string;
  136. getHMAC(format: "BYTES"): string;
  137. getHMAC(format: "UINT8ARRAY"): Uint8Array;
  138. getHMAC(format: "ARRAYBUFFER"): ArrayBuffer;
  139. /**
  140. * Internal function that returns the "raw" HMAC
  141. */
  142. protected _getHMAC(): number[];
  143. }
  144. /**
  145. * Int_64 is a object for 2 32-bit numbers emulating a 64-bit number.
  146. */
  147. declare class Int_64 {
  148. /**
  149. * @param msint_32 The most significant 32-bits of a 64-bit number.
  150. * @param lsint_32 The least significant 32-bits of a 64-bit number.
  151. */
  152. readonly highOrder: number;
  153. readonly lowOrder: number;
  154. constructor(msint_32: number, lsint_32: number);
  155. }
  156. type VariantType = "SHA-384" | "SHA-512";
  157. declare class jsSHA extends jsSHABase<Int_64[], VariantType> {
  158. intermediateState: Int_64[];
  159. variantBlockSize: number;
  160. bigEndianMod: -1 | 1;
  161. outputBinLen: number;
  162. isVariableLen: boolean;
  163. HMACSupported: boolean;
  164. converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue;
  165. roundFunc: (block: number[], H: Int_64[]) => Int_64[];
  166. finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: Int_64[]) => number[];
  167. stateCloneFunc: (state: Int_64[]) => Int_64[];
  168. newStateFunc: (variant: VariantType) => Int_64[];
  169. getMAC: () => number[];
  170. constructor(variant: VariantType, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType);
  171. constructor(variant: VariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType);
  172. }
  173. export { jsSHA as default };