UNPKG

6.35 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}
34
35declare abstract class jsSHABase<StateT, VariantT> {
36 /**
37 * @param variant The desired SHA variant.
38 * @param inputFormat The input format to be used in future `update` calls.
39 * @param options Hashmap of extra input options.
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
145declare type VariantType = "SHA-224" | "SHA-256";
146declare class jsSHA extends jsSHABase<number[], VariantType> {
147 intermediateState: number[];
148 variantBlockSize: number;
149 bigEndianMod: -1 | 1;
150 outputBinLen: number;
151 isVariableLen: boolean;
152 HMACSupported: boolean;
153 converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue;
154 roundFunc: (block: number[], H: number[]) => number[];
155 finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: number[]) => number[];
156 stateCloneFunc: (state: number[]) => number[];
157 newStateFunc: (variant: VariantType) => number[];
158 getMAC: () => number[];
159 constructor(variant: VariantType, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType);
160 constructor(variant: VariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType);
161}
162
163export default jsSHA;