UNPKG

9.15 kBTypeScriptView Raw
1declare type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE";
2declare type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY";
3declare type FormatType = "TEXT" | FormatNoTextType;
4declare 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};
18declare type FixedLengthOptionsNoEncodingType = {
19 hmacKey?: GenericInputType;
20} | {
21 numRounds?: number;
22};
23declare type FixedLengthOptionsEncodingType = {
24 hmacKey?: GenericInputType;
25 encoding?: EncodingType;
26} | {
27 numRounds?: number;
28 encoding?: EncodingType;
29};
30interface packedValue {
31 value: number[];
32 binLen: number;
33}
34interface SHAKEOptionsNoEncodingType {
35 numRounds?: number;
36}
37interface SHAKEOptionsEncodingType extends SHAKEOptionsNoEncodingType {
38 encoding?: EncodingType;
39}
40interface CSHAKEOptionsNoEncodingType {
41 customization?: GenericInputType;
42 funcName?: GenericInputType;
43}
44interface CSHAKEOptionsEncodingType extends CSHAKEOptionsNoEncodingType {
45 encoding?: EncodingType;
46}
47interface KMACOptionsNoEncodingType {
48 kmacKey: GenericInputType;
49 customization?: GenericInputType;
50}
51interface KMACOptionsEncodingType extends KMACOptionsNoEncodingType {
52 encoding?: EncodingType;
53}
54
55declare abstract class jsSHABase<StateT, VariantT> {
56 /**
57 * @param variant The desired SHA variant.
58 * @param inputFormat The input format to be used in future `update` calls.
59 * @param options Hashmap of extra input options.
60 */
61 protected readonly shaVariant: VariantT;
62 protected readonly inputFormat: FormatType;
63 protected readonly utfType: EncodingType;
64 protected readonly numRounds: number;
65 protected abstract intermediateState: StateT;
66 protected keyWithIPad: number[];
67 protected keyWithOPad: number[];
68 protected remainder: number[];
69 protected remainderLen: number;
70 protected updateCalled: boolean;
71 protected processedLen: number;
72 protected macKeySet: boolean;
73 protected abstract readonly variantBlockSize: number;
74 protected abstract readonly bigEndianMod: -1 | 1;
75 protected abstract readonly outputBinLen: number;
76 protected abstract readonly isVariableLen: boolean;
77 protected abstract readonly HMACSupported: boolean;
78 protected abstract readonly converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue;
79 protected abstract readonly roundFunc: (block: number[], H: StateT) => StateT;
80 protected abstract readonly finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: StateT, outputLen: number) => number[];
81 protected abstract readonly stateCloneFunc: (state: StateT) => StateT;
82 protected abstract readonly newStateFunc: (variant: VariantT) => StateT;
83 protected abstract readonly getMAC: ((options: {
84 outputLen: number;
85 }) => number[]) | null;
86 protected constructor(variant: VariantT, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType);
87 protected constructor(variant: VariantT, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType);
88 /**
89 * Hashes as many blocks as possible. Stores the rest for either a future update or getHash call.
90 *
91 * @param srcString The input to be hashed.
92 */
93 update(srcString: string | ArrayBuffer | Uint8Array): void;
94 /**
95 * Returns the desired SHA hash of the input fed in via `update` calls.
96 *
97 * @param format The desired output formatting
98 * @param options Hashmap of output formatting options. `outputLen` must be specified for variable length hashes.
99 * `outputLen` replaces the now deprecated `shakeLen` key.
100 * @returns The hash in the format specified.
101 */
102 getHash(format: "HEX", options?: {
103 outputUpper?: boolean;
104 outputLen?: number;
105 shakeLen?: number;
106 }): string;
107 getHash(format: "B64", options?: {
108 b64Pad?: string;
109 outputLen?: number;
110 shakeLen?: number;
111 }): string;
112 getHash(format: "BYTES", options?: {
113 outputLen?: number;
114 shakeLen?: number;
115 }): string;
116 getHash(format: "UINT8ARRAY", options?: {
117 outputLen?: number;
118 shakeLen?: number;
119 }): Uint8Array;
120 getHash(format: "ARRAYBUFFER", options?: {
121 outputLen?: number;
122 shakeLen?: number;
123 }): ArrayBuffer;
124 /**
125 * Sets the HMAC key for an eventual `getHMAC` call. Must be called immediately after jsSHA object instantiation.
126 *
127 * @param key The key used to calculate the HMAC
128 * @param inputFormat The format of key.
129 * @param options Hashmap of extra input options.
130 */
131 setHMACKey(key: string, inputFormat: "TEXT", options?: {
132 encoding?: EncodingType;
133 }): void;
134 setHMACKey(key: string, inputFormat: "B64" | "HEX" | "BYTES"): void;
135 setHMACKey(key: ArrayBuffer, inputFormat: "ARRAYBUFFER"): void;
136 setHMACKey(key: Uint8Array, inputFormat: "UINT8ARRAY"): void;
137 /**
138 * Internal function that sets the MAC key.
139 *
140 * @param key The packed MAC key to use
141 */
142 protected _setHMACKey(key: packedValue): void;
143 /**
144 * Returns the the HMAC in the specified format using the key given by a previous `setHMACKey` call.
145 *
146 * @param format The desired output formatting.
147 * @param options Hashmap of extra outputs options.
148 * @returns The HMAC in the format specified.
149 */
150 getHMAC(format: "HEX", options?: {
151 outputUpper?: boolean;
152 }): string;
153 getHMAC(format: "B64", options?: {
154 b64Pad?: string;
155 }): string;
156 getHMAC(format: "BYTES"): string;
157 getHMAC(format: "UINT8ARRAY"): Uint8Array;
158 getHMAC(format: "ARRAYBUFFER"): ArrayBuffer;
159 /**
160 * Internal function that returns the "raw" HMAC
161 */
162 protected _getHMAC(): number[];
163}
164
165/**
166 * Int_64 is a object for 2 32-bit numbers emulating a 64-bit number.
167 */
168declare class Int_64 {
169 /**
170 * @param msint_32 The most significant 32-bits of a 64-bit number.
171 * @param lsint_32 The least significant 32-bits of a 64-bit number.
172 */
173 readonly highOrder: number;
174 readonly lowOrder: number;
175 constructor(msint_32: number, lsint_32: number);
176}
177
178declare type FixedLengthVariantType = "SHA3-224" | "SHA3-256" | "SHA3-384" | "SHA3-512" | "SHAKE128" | "SHAKE256";
179declare type VariantType = FixedLengthVariantType | "SHAKE128" | "SHAKE256" | "CSHAKE128" | "CSHAKE256" | "KMAC128" | "KMAC256";
180declare class jsSHA extends jsSHABase<Int_64[][], VariantType> {
181 intermediateState: Int_64[][];
182 variantBlockSize: number;
183 bigEndianMod: -1 | 1;
184 outputBinLen: number;
185 isVariableLen: boolean;
186 HMACSupported: boolean;
187 converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue;
188 roundFunc: (block: number[], H: Int_64[][]) => Int_64[][];
189 finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: Int_64[][], outputLen: number) => number[];
190 stateCloneFunc: (state: Int_64[][]) => Int_64[][];
191 newStateFunc: (variant: VariantType) => Int_64[][];
192 getMAC: ((options: {
193 outputLen: number;
194 }) => number[]) | null;
195 constructor(variant: FixedLengthVariantType, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType);
196 constructor(variant: FixedLengthVariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType);
197 constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: "TEXT", options?: SHAKEOptionsEncodingType);
198 constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: FormatNoTextType, options?: SHAKEOptionsNoEncodingType);
199 constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: "TEXT", options?: CSHAKEOptionsEncodingType);
200 constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: FormatNoTextType, options?: CSHAKEOptionsNoEncodingType);
201 constructor(variant: "KMAC128" | "KMAC256", inputFormat: "TEXT", options: KMACOptionsEncodingType);
202 constructor(variant: "KMAC128" | "KMAC256", inputFormat: FormatNoTextType, options: KMACOptionsNoEncodingType);
203 /**
204 * Initialize CSHAKE variants.
205 *
206 * @param options Options containing CSHAKE params.
207 * @param funcNameOverride Overrides any "funcName" present in `options` (used with KMAC)
208 * @returns The delimiter to be used
209 */
210 protected _initializeCSHAKE(options?: CSHAKEOptionsNoEncodingType, funcNameOverride?: packedValue): number;
211 /**
212 * Initialize KMAC variants.
213 *
214 * @param options Options containing KMAC params.
215 */
216 protected _initializeKMAC(options: KMACOptionsNoEncodingType): void;
217 /**
218 * Returns the the KMAC in the specified format.
219 *
220 * @param options Hashmap of extra outputs options. `outputLen` must be specified.
221 * @returns The KMAC in the format specified.
222 */
223 protected _getKMAC(options: {
224 outputLen: number;
225 }): number[];
226}
227
228export default jsSHA;