UNPKG

7.32 kBTypeScriptView Raw
1type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE";
2type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY";
3type GenericInputType = {
4 value: string;
5 format: "TEXT";
6 encoding?: EncodingType;
7} | {
8 value: string;
9 format: "B64" | "HEX" | "BYTES";
10} | {
11 value: ArrayBuffer;
12 format: "ARRAYBUFFER";
13} | {
14 value: Uint8Array;
15 format: "UINT8ARRAY";
16};
17type FixedLengthOptionsNoEncodingType = {
18 hmacKey?: GenericInputType;
19} | {
20 numRounds?: number;
21};
22type FixedLengthOptionsEncodingType = {
23 hmacKey?: GenericInputType;
24 encoding?: EncodingType;
25} | {
26 numRounds?: number;
27 encoding?: EncodingType;
28};
29interface SHAKEOptionsNoEncodingType {
30 numRounds?: number;
31}
32interface SHAKEOptionsEncodingType extends SHAKEOptionsNoEncodingType {
33 encoding?: EncodingType;
34}
35interface CSHAKEOptionsNoEncodingType {
36 customization?: GenericInputType;
37 funcName?: GenericInputType;
38}
39interface CSHAKEOptionsEncodingType extends CSHAKEOptionsNoEncodingType {
40 encoding?: EncodingType;
41}
42interface KMACOptionsNoEncodingType {
43 kmacKey: GenericInputType;
44 customization?: GenericInputType;
45}
46interface KMACOptionsEncodingType extends KMACOptionsNoEncodingType {
47 encoding?: EncodingType;
48}
49
50type FixedLengthVariantType = "SHA-1" | "SHA-224" | "SHA-256" | "SHA-384" | "SHA-512" | "SHA3-224" | "SHA3-256" | "SHA3-384" | "SHA3-512";
51declare class jsSHA {
52 private readonly shaObj;
53 /**
54 * @param variant The desired SHA variant (SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA3-224, SHA3-256, SHA3-256,
55 * SHA3-384, SHA3-512, SHAKE128, SHAKE256, CSHAKE128, CSHAKE256, KMAC128, or KMAC256) as a string.
56 * @param inputFormat The input format to be used in future `update` calls (TEXT, HEX, B64, BYTES, ARRAYBUFFER,
57 * or UINT8ARRAY) as a string.
58 * @param options Options in the form of { encoding?: "UTF8" | "UTF16BE" | "UTF16LE"; numRounds?: number }.
59 * `encoding` is for only TEXT input (defaults to UTF8) and `numRounds` defaults to 1.
60 * `numRounds` is not valid for any of the MAC or CSHAKE variants.
61 * * If the variant supports HMAC, `options` may have an additional `hmacKey` key which must be in the form of
62 * {value: <INPUT>, format: <FORMAT>, encoding?: "UTF8" | "UTF16BE" | "UTF16LE"} where <FORMAT> takes the same
63 * values as `inputFormat` and <INPUT> can be a `string | ArrayBuffer | Uint8Array` depending on <FORMAT>.
64 * Supplying this key switches to HMAC calculation and replaces the now deprecated call to `setHMACKey`.
65 * * If the variant is CSHAKE128 or CSHAKE256, `options` may have two additional keys, `customization` and `funcName`,
66 * which are the NIST customization and function-name strings. Both must be in the same form as `hmacKey`.
67 * * If the variant is KMAC128 or KMAC256, `options` can include the `customization` key from CSHAKE variants and
68 * *must* have a `kmacKey` key that takes the same form as the `customization` key.
69 */
70 constructor(variant: FixedLengthVariantType, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType);
71 constructor(variant: FixedLengthVariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType);
72 constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: "TEXT", options?: SHAKEOptionsEncodingType);
73 constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: FormatNoTextType, options?: SHAKEOptionsNoEncodingType);
74 constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: "TEXT", options?: CSHAKEOptionsEncodingType);
75 constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: FormatNoTextType, options?: CSHAKEOptionsNoEncodingType);
76 constructor(variant: "KMAC128" | "KMAC256", inputFormat: "TEXT", options: KMACOptionsEncodingType);
77 constructor(variant: "KMAC128" | "KMAC256", inputFormat: FormatNoTextType, options: KMACOptionsNoEncodingType);
78 /**
79 * Takes `input` and hashes as many blocks as possible. Stores the rest for either a future `update` or `getHash` call.
80 *
81 * @param input The input to be hashed.
82 * @returns A reference to the object.
83 */
84 update(input: string | ArrayBuffer | Uint8Array): this;
85 /**
86 * Returns the desired SHA or MAC (if a HMAC/KMAC key was specified) hash of the input fed in via `update` calls.
87 *
88 * @param format The desired output formatting (B64, HEX, BYTES, ARRAYBUFFER, or UINT8ARRAY) as a string.
89 * @param options Options in the form of { outputUpper?: boolean; b64Pad?: string; outputLen?: number; }.
90 * `outputLen` is required for variable length output variants (this option was previously called `shakeLen` which
91 * is now deprecated).
92 * `outputUpper` is only for HEX output (defaults to false) and b64pad is only for B64 output (defaults to "=").
93 * @returns The hash in the format specified.
94 */
95 getHash(format: "HEX", options?: {
96 outputUpper?: boolean;
97 outputLen?: number;
98 shakeLen?: number;
99 }): string;
100 getHash(format: "B64", options?: {
101 b64Pad?: string;
102 outputLen?: number;
103 shakeLen?: number;
104 }): string;
105 getHash(format: "BYTES", options?: {
106 outputLen?: number;
107 shakeLen?: number;
108 }): string;
109 getHash(format: "UINT8ARRAY", options?: {
110 outputLen?: number;
111 shakeLen?: number;
112 }): Uint8Array;
113 getHash(format: "ARRAYBUFFER", options?: {
114 outputLen?: number;
115 shakeLen?: number;
116 }): ArrayBuffer;
117 /**
118 * Sets the HMAC key for an eventual `getHMAC` call. Must be called immediately after jsSHA object instantiation.
119 * Now deprecated in favor of setting the `hmacKey` at object instantiation.
120 *
121 * @param key The key used to calculate the HMAC
122 * @param inputFormat The format of key (HEX, TEXT, B64, BYTES, ARRAYBUFFER, or UINT8ARRAY) as a string.
123 * @param options Options in the form of { encoding?: "UTF8" | "UTF16BE" | "UTF16LE }. `encoding` is only for TEXT
124 * and defaults to UTF8.
125 */
126 setHMACKey(key: string, inputFormat: "TEXT", options?: {
127 encoding?: EncodingType;
128 }): void;
129 setHMACKey(key: string, inputFormat: "B64" | "HEX" | "BYTES"): void;
130 setHMACKey(key: ArrayBuffer, inputFormat: "ARRAYBUFFER"): void;
131 setHMACKey(key: Uint8Array, inputFormat: "UINT8ARRAY"): void;
132 /**
133 * Returns the the HMAC in the specified format using the key given by a previous `setHMACKey` call. Now deprecated
134 * in favor of just calling `getHash`.
135 *
136 * @param format The desired output formatting (B64, HEX, BYTES, ARRAYBUFFER, or UINT8ARRAY) as a string.
137 * @param options Options in the form of { outputUpper?: boolean; b64Pad?: string }. `outputUpper` is only for HEX
138 * output (defaults to false) and `b64pad` is only for B64 output (defaults to "=").
139 * @returns The HMAC in the format specified.
140 */
141 getHMAC(format: "HEX", options?: {
142 outputUpper?: boolean;
143 }): string;
144 getHMAC(format: "B64", options?: {
145 b64Pad?: string;
146 }): string;
147 getHMAC(format: "BYTES"): string;
148 getHMAC(format: "UINT8ARRAY"): Uint8Array;
149 getHMAC(format: "ARRAYBUFFER"): ArrayBuffer;
150}
151
152export { jsSHA as default };