UNPKG

7.72 kBPlain TextView Raw
1import { sha_variant_error } from "./common";
2import {
3 CSHAKEOptionsEncodingType,
4 CSHAKEOptionsNoEncodingType,
5 SHAKEOptionsEncodingType,
6 SHAKEOptionsNoEncodingType,
7 EncodingType,
8 FixedLengthOptionsEncodingType,
9 FixedLengthOptionsNoEncodingType,
10 FormatNoTextType,
11 KMACOptionsNoEncodingType,
12 KMACOptionsEncodingType,
13} from "./custom_types";
14import jsSHA1 from "./sha1";
15import jsSHA256 from "./sha256";
16import jsSHA512 from "./sha512";
17import jsSHA3 from "./sha3";
18
19type 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
30export default class jsSHA {
31 private readonly shaObj: jsSHA1 | jsSHA256 | jsSHA512 | jsSHA3;
32 /**
33 * @param variant The desired SHA variant (SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA3-224, SHA3-256, SHA3-256,
34 * SHA3-384, SHA3-512, SHAKE128, SHAKE256, CSHAKE128, CSHAKE256, KMAC128, or KMAC256) as a string.
35 * @param inputFormat The input format to be used in future `update` calls (TEXT, HEX, B64, BYTES, ARRAYBUFFER,
36 * or UINT8ARRAY) as a string.
37 * @param options Options in the form of { encoding?: "UTF8" | "UTF16BE" | "UTF16LE"; numRounds?: number }.
38 * `encoding` is for only TEXT input (defaults to UTF8) and `numRounds` defaults to 1.
39 * `numRounds` is not valid for any of the MAC or CSHAKE variants.
40 * * If the variant supports HMAC, `options` may have an additional `hmacKey` key which must be in the form of
41 * {value: <INPUT>, format: <FORMAT>, encoding?: "UTF8" | "UTF16BE" | "UTF16LE"} where <FORMAT> takes the same
42 * values as `inputFormat` and <INPUT> can be a `string | ArrayBuffer | Uint8Array` depending on <FORMAT>.
43 * Supplying this key switches to HMAC calculation and replaces the now deprecated call to `setHMACKey`.
44 * * If the variant is CSHAKE128 or CSHAKE256, `options` may have two additional keys, `customization` and `funcName`,
45 * which are the NIST customization and function-name strings. Both must be in the same form as `hmacKey`.
46 * * If the variant is KMAC128 or KMAC256, `options` can include the `customization` key from CSHAKE variants and
47 * *must* have a `kmacKey` key that takes the same form as the `customization` key.
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 * Sets the HMAC key for an eventual `getHMAC` call. Must be called immediately after jsSHA object instantiation.
118 * Now deprecated in favor of setting the `hmacKey` at object instantiation.
119 *
120 * @param key The key used to calculate the HMAC
121 * @param inputFormat The format of key (HEX, TEXT, B64, BYTES, ARRAYBUFFER, or UINT8ARRAY) as a string.
122 * @param options Options in the form of { encoding?: "UTF8" | "UTF16BE" | "UTF16LE }. `encoding` is only for TEXT
123 * and defaults to UTF8.
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 // eslint-disable-next-line @typescript-eslint/no-explicit-any
130 setHMACKey(key: any, inputFormat: any, options?: any): void {
131 this.shaObj.setHMACKey(key, inputFormat, options);
132 }
133
134 /**
135 * Returns the the HMAC in the specified format using the key given by a previous `setHMACKey` call. Now deprecated
136 * in favor of just calling `getHash`.
137 *
138 * @param format The desired output formatting (B64, HEX, BYTES, ARRAYBUFFER, or UINT8ARRAY) as a string.
139 * @param options Options in the form of { outputUpper?: boolean; b64Pad?: string }. `outputUpper` is only for HEX
140 * output (defaults to false) and `b64pad` is only for B64 output (defaults to "=").
141 * @returns The HMAC in the format specified.
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 // eslint-disable-next-line @typescript-eslint/no-explicit-any
149 getHMAC(format: any, options?: any): any {
150 return this.shaObj.getHMAC(format, options);
151 }
152}