1 | import { sha_variant_error } from "./common";
|
2 | import {
|
3 | CSHAKEOptionsEncodingType,
|
4 | CSHAKEOptionsNoEncodingType,
|
5 | SHAKEOptionsEncodingType,
|
6 | SHAKEOptionsNoEncodingType,
|
7 | EncodingType,
|
8 | FixedLengthOptionsEncodingType,
|
9 | FixedLengthOptionsNoEncodingType,
|
10 | FormatNoTextType,
|
11 | KMACOptionsNoEncodingType,
|
12 | KMACOptionsEncodingType,
|
13 | } from "./custom_types";
|
14 | import jsSHA1 from "./sha1";
|
15 | import jsSHA256 from "./sha256";
|
16 | import jsSHA512 from "./sha512";
|
17 | import jsSHA3 from "./sha3";
|
18 |
|
19 | type FixedLengthVariantType =
|
20 | | "SHA-1"
|
21 | | "SHA-224"
|
22 | | "SHA-256"
|
23 | | "SHA-384"
|
24 | | "SHA-512"
|
25 | | "SHA3-224"
|
26 | | "SHA3-256"
|
27 | | "SHA3-384"
|
28 | | "SHA3-512";
|
29 |
|
30 | export default class jsSHA {
|
31 | private readonly shaObj: jsSHA1 | jsSHA256 | jsSHA512 | jsSHA3;
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | constructor(variant: FixedLengthVariantType, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType);
|
50 | constructor(
|
51 | variant: FixedLengthVariantType,
|
52 | inputFormat: FormatNoTextType,
|
53 | options?: FixedLengthOptionsNoEncodingType
|
54 | );
|
55 | constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: "TEXT", options?: SHAKEOptionsEncodingType);
|
56 | constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: FormatNoTextType, options?: SHAKEOptionsNoEncodingType);
|
57 | constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: "TEXT", options?: CSHAKEOptionsEncodingType);
|
58 | constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: FormatNoTextType, options?: CSHAKEOptionsNoEncodingType);
|
59 | constructor(variant: "KMAC128" | "KMAC256", inputFormat: "TEXT", options: KMACOptionsEncodingType);
|
60 | constructor(variant: "KMAC128" | "KMAC256", inputFormat: FormatNoTextType, options: KMACOptionsNoEncodingType);
|
61 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
62 | constructor(variant: any, inputFormat: any, options?: any) {
|
63 | if ("SHA-1" == variant) {
|
64 | this.shaObj = new jsSHA1(variant, inputFormat, options);
|
65 | } else if ("SHA-224" == variant || "SHA-256" == variant) {
|
66 | this.shaObj = new jsSHA256(variant, inputFormat, options);
|
67 | } else if ("SHA-384" == variant || "SHA-512" == variant) {
|
68 | this.shaObj = new jsSHA512(variant, inputFormat, options);
|
69 | } else if (
|
70 | "SHA3-224" == variant ||
|
71 | "SHA3-256" == variant ||
|
72 | "SHA3-384" == variant ||
|
73 | "SHA3-512" == variant ||
|
74 | "SHAKE128" == variant ||
|
75 | "SHAKE256" == variant ||
|
76 | "CSHAKE128" == variant ||
|
77 | "CSHAKE256" == variant ||
|
78 | "KMAC128" == variant ||
|
79 | "KMAC256" == variant
|
80 | ) {
|
81 | this.shaObj = new jsSHA3(variant, inputFormat, options);
|
82 | } else {
|
83 | throw new Error(sha_variant_error);
|
84 | }
|
85 | }
|
86 |
|
87 | /**
|
88 | * Takes `input` and hashes as many blocks as possible. Stores the rest for either a future `update` or `getHash` call.
|
89 | *
|
90 | * @param input The input to be hashed
|
91 | */
|
92 | update(input: string | ArrayBuffer | Uint8Array): void {
|
93 | this.shaObj.update(input);
|
94 | }
|
95 |
|
96 | /**
|
97 | * Returns the desired SHA or MAC (if a HMAC/KMAC key was specified) hash of the input fed in via `update` calls.
|
98 | *
|
99 | * @param format The desired output formatting (B64, HEX, BYTES, ARRAYBUFFER, or UINT8ARRAY) as a string.
|
100 | * @param options Options in the form of { outputUpper?: boolean; b64Pad?: string; outputLen?: number; }.
|
101 | * `outputLen` is required for variable length output variants (this option was previously called `shakeLen` which
|
102 | * is now deprecated).
|
103 | * `outputUpper` is only for HEX output (defaults to false) and b64pad is only for B64 output (defaults to "=").
|
104 | * @returns The hash in the format specified.
|
105 | */
|
106 | getHash(format: "HEX", options?: { outputUpper?: boolean; outputLen?: number; shakeLen?: number }): string;
|
107 | getHash(format: "B64", options?: { b64Pad?: string; outputLen?: number; shakeLen?: number }): string;
|
108 | getHash(format: "BYTES", options?: { outputLen?: number; shakeLen?: number }): string;
|
109 | getHash(format: "UINT8ARRAY", options?: { outputLen?: number; shakeLen?: number }): Uint8Array;
|
110 | getHash(format: "ARRAYBUFFER", options?: { outputLen?: number; shakeLen?: number }): ArrayBuffer;
|
111 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
112 | getHash(format: any, options?: any): any {
|
113 | return this.shaObj.getHash(format, options);
|
114 | }
|
115 |
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | setHMACKey(key: string, inputFormat: "TEXT", options?: { encoding?: EncodingType }): void;
|
126 | setHMACKey(key: string, inputFormat: "B64" | "HEX" | "BYTES"): void;
|
127 | setHMACKey(key: ArrayBuffer, inputFormat: "ARRAYBUFFER"): void;
|
128 | setHMACKey(key: Uint8Array, inputFormat: "UINT8ARRAY"): void;
|
129 |
|
130 | setHMACKey(key: any, inputFormat: any, options?: any): void {
|
131 | this.shaObj.setHMACKey(key, inputFormat, options);
|
132 | }
|
133 |
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | getHMAC(format: "HEX", options?: { outputUpper?: boolean }): string;
|
144 | getHMAC(format: "B64", options?: { b64Pad?: string }): string;
|
145 | getHMAC(format: "BYTES"): string;
|
146 | getHMAC(format: "UINT8ARRAY"): Uint8Array;
|
147 | getHMAC(format: "ARRAYBUFFER"): ArrayBuffer;
|
148 |
|
149 | getHMAC(format: any, options?: any): any {
|
150 | return this.shaObj.getHMAC(format, options);
|
151 | }
|
152 | }
|