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 |
|
35 | declare abstract class jsSHABase<StateT, VariantT> {
|
36 | |
37 |
|
38 |
|
39 |
|
40 |
|
41 | protected readonly shaVariant: VariantT;
|
42 | protected readonly inputFormat: FormatType;
|
43 | protected readonly utfType: EncodingType;
|
44 | protected readonly numRounds: number;
|
45 | protected abstract intermediateState: StateT;
|
46 | protected keyWithIPad: number[];
|
47 | protected keyWithOPad: number[];
|
48 | protected remainder: number[];
|
49 | protected remainderLen: number;
|
50 | protected updateCalled: boolean;
|
51 | protected processedLen: number;
|
52 | protected macKeySet: boolean;
|
53 | protected abstract readonly variantBlockSize: number;
|
54 | protected abstract readonly bigEndianMod: -1 | 1;
|
55 | protected abstract readonly outputBinLen: number;
|
56 | protected abstract readonly isVariableLen: boolean;
|
57 | protected abstract readonly HMACSupported: boolean;
|
58 | protected abstract readonly converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue;
|
59 | protected abstract readonly roundFunc: (block: number[], H: StateT) => StateT;
|
60 | protected abstract readonly finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: StateT, outputLen: number) => number[];
|
61 | protected abstract readonly stateCloneFunc: (state: StateT) => StateT;
|
62 | protected abstract readonly newStateFunc: (variant: VariantT) => StateT;
|
63 | protected abstract readonly getMAC: ((options: {
|
64 | outputLen: number;
|
65 | }) => number[]) | null;
|
66 | protected constructor(variant: VariantT, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType);
|
67 | protected constructor(variant: VariantT, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType);
|
68 | /**
|
69 | * Hashes as many blocks as possible. Stores the rest for either a future update or getHash call.
|
70 | *
|
71 | * @param srcString The input to be hashed.
|
72 | */
|
73 | update(srcString: string | ArrayBuffer | Uint8Array): void;
|
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 | /**
|
146 | * Int_64 is a object for 2 32-bit numbers emulating a 64-bit number.
|
147 | */
|
148 | declare class Int_64 {
|
149 | /**
|
150 | * @param msint_32 The most significant 32-bits of a 64-bit number.
|
151 | * @param lsint_32 The least significant 32-bits of a 64-bit number.
|
152 | */
|
153 | readonly highOrder: number;
|
154 | readonly lowOrder: number;
|
155 | constructor(msint_32: number, lsint_32: number);
|
156 | }
|
157 |
|
158 | declare type VariantType = "SHA-384" | "SHA-512";
|
159 | declare class jsSHA extends jsSHABase<Int_64[], VariantType> {
|
160 | intermediateState: Int_64[];
|
161 | variantBlockSize: number;
|
162 | bigEndianMod: -1 | 1;
|
163 | outputBinLen: number;
|
164 | isVariableLen: boolean;
|
165 | HMACSupported: boolean;
|
166 | converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue;
|
167 | roundFunc: (block: number[], H: Int_64[]) => Int_64[];
|
168 | finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: Int_64[]) => number[];
|
169 | stateCloneFunc: (state: Int_64[]) => Int_64[];
|
170 | newStateFunc: (variant: VariantType) => Int_64[];
|
171 | getMAC: () => number[];
|
172 | constructor(variant: VariantType, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType);
|
173 | constructor(variant: VariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType);
|
174 | }
|
175 |
|
176 | export default jsSHA;
|