From fbccaf167d990c6997b3ff463f45f59981a7a7b0 Mon Sep 17 00:00:00 2001 From: Brian Turek <2017685+Caligatio@users.noreply.github.com> Date: Wed, 17 Jun 2026 07:05:27 +0200 Subject: [PATCH] Fix the TS definitions by adding an additional build/fix-up step --- dist/sha.d.cts | 152 +++++++++++++++++++++ dist/sha.d.mts | 152 +++++++++++++++++++++ dist/sha.d.ts | 2 +- dist/sha1.d.cts | 163 +++++++++++++++++++++++ dist/sha1.d.mts | 163 +++++++++++++++++++++++ dist/sha1.d.ts | 2 +- dist/sha256.d.cts | 164 +++++++++++++++++++++++ dist/sha256.d.mts | 164 +++++++++++++++++++++++ dist/sha256.d.ts | 2 +- dist/sha3.d.cts | 229 ++++++++++++++++++++++++++++++++ dist/sha3.d.mts | 229 ++++++++++++++++++++++++++++++++ dist/sha3.d.ts | 2 +- dist/sha512.d.cts | 177 ++++++++++++++++++++++++ dist/sha512.d.mts | 177 ++++++++++++++++++++++++ dist/sha512.d.ts | 2 +- package.json | 102 ++++++++++---- scripts/generate-dual-types.mjs | 31 +++++ 17 files changed, 1880 insertions(+), 33 deletions(-) create mode 100644 dist/sha.d.cts create mode 100644 dist/sha.d.mts create mode 100644 dist/sha1.d.cts create mode 100644 dist/sha1.d.mts create mode 100644 dist/sha256.d.cts create mode 100644 dist/sha256.d.mts create mode 100644 dist/sha3.d.cts create mode 100644 dist/sha3.d.mts create mode 100644 dist/sha512.d.cts create mode 100644 dist/sha512.d.mts create mode 100644 scripts/generate-dual-types.mjs diff --git a/dist/sha.d.cts b/dist/sha.d.cts new file mode 100644 index 0000000..65f947d --- /dev/null +++ b/dist/sha.d.cts @@ -0,0 +1,152 @@ +type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE"; +type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY"; +type GenericInputType = { + value: string; + format: "TEXT"; + encoding?: EncodingType; +} | { + value: string; + format: "B64" | "HEX" | "BYTES"; +} | { + value: ArrayBuffer; + format: "ARRAYBUFFER"; +} | { + value: Uint8Array; + format: "UINT8ARRAY"; +}; +type FixedLengthOptionsNoEncodingType = { + hmacKey?: GenericInputType; +} | { + numRounds?: number; +}; +type FixedLengthOptionsEncodingType = { + hmacKey?: GenericInputType; + encoding?: EncodingType; +} | { + numRounds?: number; + encoding?: EncodingType; +}; +interface SHAKEOptionsNoEncodingType { + numRounds?: number; +} +interface SHAKEOptionsEncodingType extends SHAKEOptionsNoEncodingType { + encoding?: EncodingType; +} +interface CSHAKEOptionsNoEncodingType { + customization?: GenericInputType; + funcName?: GenericInputType; +} +interface CSHAKEOptionsEncodingType extends CSHAKEOptionsNoEncodingType { + encoding?: EncodingType; +} +interface KMACOptionsNoEncodingType { + kmacKey: GenericInputType; + customization?: GenericInputType; +} +interface KMACOptionsEncodingType extends KMACOptionsNoEncodingType { + encoding?: EncodingType; +} + +type FixedLengthVariantType = "SHA-1" | "SHA-224" | "SHA-256" | "SHA-384" | "SHA-512" | "SHA3-224" | "SHA3-256" | "SHA3-384" | "SHA3-512"; +declare class jsSHA { + private readonly shaObj; + /** + * @param variant The desired SHA variant (SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA3-224, SHA3-256, SHA3-256, + * SHA3-384, SHA3-512, SHAKE128, SHAKE256, CSHAKE128, CSHAKE256, KMAC128, or KMAC256) as a string. + * @param inputFormat The input format to be used in future `update` calls (TEXT, HEX, B64, BYTES, ARRAYBUFFER, + * or UINT8ARRAY) as a string. + * @param options Options in the form of { encoding?: "UTF8" | "UTF16BE" | "UTF16LE"; numRounds?: number }. + * `encoding` is for only TEXT input (defaults to UTF8) and `numRounds` defaults to 1. + * `numRounds` is not valid for any of the MAC or CSHAKE variants. + * * If the variant supports HMAC, `options` may have an additional `hmacKey` key which must be in the form of + * {value: , format: , encoding?: "UTF8" | "UTF16BE" | "UTF16LE"} where takes the same + * values as `inputFormat` and can be a `string | ArrayBuffer | Uint8Array` depending on . + * Supplying this key switches to HMAC calculation and replaces the now deprecated call to `setHMACKey`. + * * If the variant is CSHAKE128 or CSHAKE256, `options` may have two additional keys, `customization` and `funcName`, + * which are the NIST customization and function-name strings. Both must be in the same form as `hmacKey`. + * * If the variant is KMAC128 or KMAC256, `options` can include the `customization` key from CSHAKE variants and + * *must* have a `kmacKey` key that takes the same form as the `customization` key. + */ + constructor(variant: FixedLengthVariantType, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + constructor(variant: FixedLengthVariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); + constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: "TEXT", options?: SHAKEOptionsEncodingType); + constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: FormatNoTextType, options?: SHAKEOptionsNoEncodingType); + constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: "TEXT", options?: CSHAKEOptionsEncodingType); + constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: FormatNoTextType, options?: CSHAKEOptionsNoEncodingType); + constructor(variant: "KMAC128" | "KMAC256", inputFormat: "TEXT", options: KMACOptionsEncodingType); + constructor(variant: "KMAC128" | "KMAC256", inputFormat: FormatNoTextType, options: KMACOptionsNoEncodingType); + /** + * Takes `input` and hashes as many blocks as possible. Stores the rest for either a future `update` or `getHash` call. + * + * @param input The input to be hashed. + * @returns A reference to the object. + */ + update(input: string | ArrayBuffer | Uint8Array): this; + /** + * Returns the desired SHA or MAC (if a HMAC/KMAC key was specified) hash of the input fed in via `update` calls. + * + * @param format The desired output formatting (B64, HEX, BYTES, ARRAYBUFFER, or UINT8ARRAY) as a string. + * @param options Options in the form of { outputUpper?: boolean; b64Pad?: string; outputLen?: number; }. + * `outputLen` is required for variable length output variants (this option was previously called `shakeLen` which + * is now deprecated). + * `outputUpper` is only for HEX output (defaults to false) and b64pad is only for B64 output (defaults to "="). + * @returns The hash in the format specified. + */ + getHash(format: "HEX", options?: { + outputUpper?: boolean; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "B64", options?: { + b64Pad?: string; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "BYTES", options?: { + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "UINT8ARRAY", options?: { + outputLen?: number; + shakeLen?: number; + }): Uint8Array; + getHash(format: "ARRAYBUFFER", options?: { + outputLen?: number; + shakeLen?: number; + }): ArrayBuffer; + /** + * Sets the HMAC key for an eventual `getHMAC` call. Must be called immediately after jsSHA object instantiation. + * Now deprecated in favor of setting the `hmacKey` at object instantiation. + * + * @param key The key used to calculate the HMAC + * @param inputFormat The format of key (HEX, TEXT, B64, BYTES, ARRAYBUFFER, or UINT8ARRAY) as a string. + * @param options Options in the form of { encoding?: "UTF8" | "UTF16BE" | "UTF16LE }. `encoding` is only for TEXT + * and defaults to UTF8. + */ + setHMACKey(key: string, inputFormat: "TEXT", options?: { + encoding?: EncodingType; + }): void; + setHMACKey(key: string, inputFormat: "B64" | "HEX" | "BYTES"): void; + setHMACKey(key: ArrayBuffer, inputFormat: "ARRAYBUFFER"): void; + setHMACKey(key: Uint8Array, inputFormat: "UINT8ARRAY"): void; + /** + * Returns the the HMAC in the specified format using the key given by a previous `setHMACKey` call. Now deprecated + * in favor of just calling `getHash`. + * + * @param format The desired output formatting (B64, HEX, BYTES, ARRAYBUFFER, or UINT8ARRAY) as a string. + * @param options Options in the form of { outputUpper?: boolean; b64Pad?: string }. `outputUpper` is only for HEX + * output (defaults to false) and `b64pad` is only for B64 output (defaults to "="). + * @returns The HMAC in the format specified. + */ + getHMAC(format: "HEX", options?: { + outputUpper?: boolean; + }): string; + getHMAC(format: "B64", options?: { + b64Pad?: string; + }): string; + getHMAC(format: "BYTES"): string; + getHMAC(format: "UINT8ARRAY"): Uint8Array; + getHMAC(format: "ARRAYBUFFER"): ArrayBuffer; +} + +export = jsSHA; diff --git a/dist/sha.d.mts b/dist/sha.d.mts new file mode 100644 index 0000000..bafe2ca --- /dev/null +++ b/dist/sha.d.mts @@ -0,0 +1,152 @@ +type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE"; +type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY"; +type GenericInputType = { + value: string; + format: "TEXT"; + encoding?: EncodingType; +} | { + value: string; + format: "B64" | "HEX" | "BYTES"; +} | { + value: ArrayBuffer; + format: "ARRAYBUFFER"; +} | { + value: Uint8Array; + format: "UINT8ARRAY"; +}; +type FixedLengthOptionsNoEncodingType = { + hmacKey?: GenericInputType; +} | { + numRounds?: number; +}; +type FixedLengthOptionsEncodingType = { + hmacKey?: GenericInputType; + encoding?: EncodingType; +} | { + numRounds?: number; + encoding?: EncodingType; +}; +interface SHAKEOptionsNoEncodingType { + numRounds?: number; +} +interface SHAKEOptionsEncodingType extends SHAKEOptionsNoEncodingType { + encoding?: EncodingType; +} +interface CSHAKEOptionsNoEncodingType { + customization?: GenericInputType; + funcName?: GenericInputType; +} +interface CSHAKEOptionsEncodingType extends CSHAKEOptionsNoEncodingType { + encoding?: EncodingType; +} +interface KMACOptionsNoEncodingType { + kmacKey: GenericInputType; + customization?: GenericInputType; +} +interface KMACOptionsEncodingType extends KMACOptionsNoEncodingType { + encoding?: EncodingType; +} + +type FixedLengthVariantType = "SHA-1" | "SHA-224" | "SHA-256" | "SHA-384" | "SHA-512" | "SHA3-224" | "SHA3-256" | "SHA3-384" | "SHA3-512"; +declare class jsSHA { + private readonly shaObj; + /** + * @param variant The desired SHA variant (SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA3-224, SHA3-256, SHA3-256, + * SHA3-384, SHA3-512, SHAKE128, SHAKE256, CSHAKE128, CSHAKE256, KMAC128, or KMAC256) as a string. + * @param inputFormat The input format to be used in future `update` calls (TEXT, HEX, B64, BYTES, ARRAYBUFFER, + * or UINT8ARRAY) as a string. + * @param options Options in the form of { encoding?: "UTF8" | "UTF16BE" | "UTF16LE"; numRounds?: number }. + * `encoding` is for only TEXT input (defaults to UTF8) and `numRounds` defaults to 1. + * `numRounds` is not valid for any of the MAC or CSHAKE variants. + * * If the variant supports HMAC, `options` may have an additional `hmacKey` key which must be in the form of + * {value: , format: , encoding?: "UTF8" | "UTF16BE" | "UTF16LE"} where takes the same + * values as `inputFormat` and can be a `string | ArrayBuffer | Uint8Array` depending on . + * Supplying this key switches to HMAC calculation and replaces the now deprecated call to `setHMACKey`. + * * If the variant is CSHAKE128 or CSHAKE256, `options` may have two additional keys, `customization` and `funcName`, + * which are the NIST customization and function-name strings. Both must be in the same form as `hmacKey`. + * * If the variant is KMAC128 or KMAC256, `options` can include the `customization` key from CSHAKE variants and + * *must* have a `kmacKey` key that takes the same form as the `customization` key. + */ + constructor(variant: FixedLengthVariantType, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + constructor(variant: FixedLengthVariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); + constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: "TEXT", options?: SHAKEOptionsEncodingType); + constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: FormatNoTextType, options?: SHAKEOptionsNoEncodingType); + constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: "TEXT", options?: CSHAKEOptionsEncodingType); + constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: FormatNoTextType, options?: CSHAKEOptionsNoEncodingType); + constructor(variant: "KMAC128" | "KMAC256", inputFormat: "TEXT", options: KMACOptionsEncodingType); + constructor(variant: "KMAC128" | "KMAC256", inputFormat: FormatNoTextType, options: KMACOptionsNoEncodingType); + /** + * Takes `input` and hashes as many blocks as possible. Stores the rest for either a future `update` or `getHash` call. + * + * @param input The input to be hashed. + * @returns A reference to the object. + */ + update(input: string | ArrayBuffer | Uint8Array): this; + /** + * Returns the desired SHA or MAC (if a HMAC/KMAC key was specified) hash of the input fed in via `update` calls. + * + * @param format The desired output formatting (B64, HEX, BYTES, ARRAYBUFFER, or UINT8ARRAY) as a string. + * @param options Options in the form of { outputUpper?: boolean; b64Pad?: string; outputLen?: number; }. + * `outputLen` is required for variable length output variants (this option was previously called `shakeLen` which + * is now deprecated). + * `outputUpper` is only for HEX output (defaults to false) and b64pad is only for B64 output (defaults to "="). + * @returns The hash in the format specified. + */ + getHash(format: "HEX", options?: { + outputUpper?: boolean; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "B64", options?: { + b64Pad?: string; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "BYTES", options?: { + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "UINT8ARRAY", options?: { + outputLen?: number; + shakeLen?: number; + }): Uint8Array; + getHash(format: "ARRAYBUFFER", options?: { + outputLen?: number; + shakeLen?: number; + }): ArrayBuffer; + /** + * Sets the HMAC key for an eventual `getHMAC` call. Must be called immediately after jsSHA object instantiation. + * Now deprecated in favor of setting the `hmacKey` at object instantiation. + * + * @param key The key used to calculate the HMAC + * @param inputFormat The format of key (HEX, TEXT, B64, BYTES, ARRAYBUFFER, or UINT8ARRAY) as a string. + * @param options Options in the form of { encoding?: "UTF8" | "UTF16BE" | "UTF16LE }. `encoding` is only for TEXT + * and defaults to UTF8. + */ + setHMACKey(key: string, inputFormat: "TEXT", options?: { + encoding?: EncodingType; + }): void; + setHMACKey(key: string, inputFormat: "B64" | "HEX" | "BYTES"): void; + setHMACKey(key: ArrayBuffer, inputFormat: "ARRAYBUFFER"): void; + setHMACKey(key: Uint8Array, inputFormat: "UINT8ARRAY"): void; + /** + * Returns the the HMAC in the specified format using the key given by a previous `setHMACKey` call. Now deprecated + * in favor of just calling `getHash`. + * + * @param format The desired output formatting (B64, HEX, BYTES, ARRAYBUFFER, or UINT8ARRAY) as a string. + * @param options Options in the form of { outputUpper?: boolean; b64Pad?: string }. `outputUpper` is only for HEX + * output (defaults to false) and `b64pad` is only for B64 output (defaults to "="). + * @returns The HMAC in the format specified. + */ + getHMAC(format: "HEX", options?: { + outputUpper?: boolean; + }): string; + getHMAC(format: "B64", options?: { + b64Pad?: string; + }): string; + getHMAC(format: "BYTES"): string; + getHMAC(format: "UINT8ARRAY"): Uint8Array; + getHMAC(format: "ARRAYBUFFER"): ArrayBuffer; +} + +export { jsSHA as default }; diff --git a/dist/sha.d.ts b/dist/sha.d.ts index bafe2ca..65f947d 100644 --- a/dist/sha.d.ts +++ b/dist/sha.d.ts @@ -149,4 +149,4 @@ declare class jsSHA { getHMAC(format: "ARRAYBUFFER"): ArrayBuffer; } -export { jsSHA as default }; +export = jsSHA; diff --git a/dist/sha1.d.cts b/dist/sha1.d.cts new file mode 100644 index 0000000..79baff5 --- /dev/null +++ b/dist/sha1.d.cts @@ -0,0 +1,163 @@ +type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE"; +type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY"; +type FormatType = "TEXT" | FormatNoTextType; +type GenericInputType = { + value: string; + format: "TEXT"; + encoding?: EncodingType; +} | { + value: string; + format: "B64" | "HEX" | "BYTES"; +} | { + value: ArrayBuffer; + format: "ARRAYBUFFER"; +} | { + value: Uint8Array; + format: "UINT8ARRAY"; +}; +type FixedLengthOptionsNoEncodingType = { + hmacKey?: GenericInputType; +} | { + numRounds?: number; +}; +type FixedLengthOptionsEncodingType = { + hmacKey?: GenericInputType; + encoding?: EncodingType; +} | { + numRounds?: number; + encoding?: EncodingType; +}; +interface packedValue { + value: number[]; + binLen: number; +} + +declare abstract class jsSHABase { + /** + * @param variant The desired SHA variant. + * @param inputFormat The input format to be used in future `update` calls. + * @param options Hashmap of extra input options. + */ + protected readonly shaVariant: VariantT; + protected readonly inputFormat: FormatType; + protected readonly utfType: EncodingType; + protected readonly numRounds: number; + protected abstract intermediateState: StateT; + protected keyWithIPad: number[]; + protected keyWithOPad: number[]; + protected remainder: number[]; + protected remainderLen: number; + protected updateCalled: boolean; + protected processedLen: number; + protected macKeySet: boolean; + protected abstract readonly variantBlockSize: number; + protected abstract readonly bigEndianMod: -1 | 1; + protected abstract readonly outputBinLen: number; + protected abstract readonly isVariableLen: boolean; + protected abstract readonly HMACSupported: boolean; + protected abstract readonly converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue; + protected abstract readonly roundFunc: (block: number[], H: StateT) => StateT; + protected abstract readonly finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: StateT, outputLen: number) => number[]; + protected abstract readonly stateCloneFunc: (state: StateT) => StateT; + protected abstract readonly newStateFunc: (variant: VariantT) => StateT; + protected abstract readonly getMAC: ((options: { + outputLen: number; + }) => number[]) | null; + protected constructor(variant: VariantT, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + protected constructor(variant: VariantT, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); + /** + * Hashes as many blocks as possible. Stores the rest for either a future update or getHash call. + * + * @param srcString The input to be hashed. + * @returns A reference to the object. + */ + update(srcString: string | ArrayBuffer | Uint8Array): this; + /** + * Returns the desired SHA hash of the input fed in via `update` calls. + * + * @param format The desired output formatting + * @param options Hashmap of output formatting options. `outputLen` must be specified for variable length hashes. + * `outputLen` replaces the now deprecated `shakeLen` key. + * @returns The hash in the format specified. + */ + getHash(format: "HEX", options?: { + outputUpper?: boolean; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "B64", options?: { + b64Pad?: string; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "BYTES", options?: { + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "UINT8ARRAY", options?: { + outputLen?: number; + shakeLen?: number; + }): Uint8Array; + getHash(format: "ARRAYBUFFER", options?: { + outputLen?: number; + shakeLen?: number; + }): ArrayBuffer; + /** + * Sets the HMAC key for an eventual `getHMAC` call. Must be called immediately after jsSHA object instantiation. + * + * @param key The key used to calculate the HMAC + * @param inputFormat The format of key. + * @param options Hashmap of extra input options. + */ + setHMACKey(key: string, inputFormat: "TEXT", options?: { + encoding?: EncodingType; + }): void; + setHMACKey(key: string, inputFormat: "B64" | "HEX" | "BYTES"): void; + setHMACKey(key: ArrayBuffer, inputFormat: "ARRAYBUFFER"): void; + setHMACKey(key: Uint8Array, inputFormat: "UINT8ARRAY"): void; + /** + * Internal function that sets the MAC key. + * + * @param key The packed MAC key to use + */ + protected _setHMACKey(key: packedValue): void; + /** + * Returns the the HMAC in the specified format using the key given by a previous `setHMACKey` call. + * + * @param format The desired output formatting. + * @param options Hashmap of extra outputs options. + * @returns The HMAC in the format specified. + */ + getHMAC(format: "HEX", options?: { + outputUpper?: boolean; + }): string; + getHMAC(format: "B64", options?: { + b64Pad?: string; + }): string; + getHMAC(format: "BYTES"): string; + getHMAC(format: "UINT8ARRAY"): Uint8Array; + getHMAC(format: "ARRAYBUFFER"): ArrayBuffer; + /** + * Internal function that returns the "raw" HMAC + */ + protected _getHMAC(): number[]; +} + +declare class jsSHA extends jsSHABase { + intermediateState: number[]; + variantBlockSize: number; + bigEndianMod: -1 | 1; + outputBinLen: number; + isVariableLen: boolean; + HMACSupported: boolean; + converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue; + roundFunc: (block: number[], H: number[]) => number[]; + finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: number[]) => number[]; + stateCloneFunc: (state: number[]) => number[]; + newStateFunc: (variant: "SHA-1") => number[]; + getMAC: () => number[]; + constructor(variant: "SHA-1", inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + constructor(variant: "SHA-1", inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); +} + +export = jsSHA; diff --git a/dist/sha1.d.mts b/dist/sha1.d.mts new file mode 100644 index 0000000..adaa861 --- /dev/null +++ b/dist/sha1.d.mts @@ -0,0 +1,163 @@ +type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE"; +type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY"; +type FormatType = "TEXT" | FormatNoTextType; +type GenericInputType = { + value: string; + format: "TEXT"; + encoding?: EncodingType; +} | { + value: string; + format: "B64" | "HEX" | "BYTES"; +} | { + value: ArrayBuffer; + format: "ARRAYBUFFER"; +} | { + value: Uint8Array; + format: "UINT8ARRAY"; +}; +type FixedLengthOptionsNoEncodingType = { + hmacKey?: GenericInputType; +} | { + numRounds?: number; +}; +type FixedLengthOptionsEncodingType = { + hmacKey?: GenericInputType; + encoding?: EncodingType; +} | { + numRounds?: number; + encoding?: EncodingType; +}; +interface packedValue { + value: number[]; + binLen: number; +} + +declare abstract class jsSHABase { + /** + * @param variant The desired SHA variant. + * @param inputFormat The input format to be used in future `update` calls. + * @param options Hashmap of extra input options. + */ + protected readonly shaVariant: VariantT; + protected readonly inputFormat: FormatType; + protected readonly utfType: EncodingType; + protected readonly numRounds: number; + protected abstract intermediateState: StateT; + protected keyWithIPad: number[]; + protected keyWithOPad: number[]; + protected remainder: number[]; + protected remainderLen: number; + protected updateCalled: boolean; + protected processedLen: number; + protected macKeySet: boolean; + protected abstract readonly variantBlockSize: number; + protected abstract readonly bigEndianMod: -1 | 1; + protected abstract readonly outputBinLen: number; + protected abstract readonly isVariableLen: boolean; + protected abstract readonly HMACSupported: boolean; + protected abstract readonly converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue; + protected abstract readonly roundFunc: (block: number[], H: StateT) => StateT; + protected abstract readonly finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: StateT, outputLen: number) => number[]; + protected abstract readonly stateCloneFunc: (state: StateT) => StateT; + protected abstract readonly newStateFunc: (variant: VariantT) => StateT; + protected abstract readonly getMAC: ((options: { + outputLen: number; + }) => number[]) | null; + protected constructor(variant: VariantT, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + protected constructor(variant: VariantT, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); + /** + * Hashes as many blocks as possible. Stores the rest for either a future update or getHash call. + * + * @param srcString The input to be hashed. + * @returns A reference to the object. + */ + update(srcString: string | ArrayBuffer | Uint8Array): this; + /** + * Returns the desired SHA hash of the input fed in via `update` calls. + * + * @param format The desired output formatting + * @param options Hashmap of output formatting options. `outputLen` must be specified for variable length hashes. + * `outputLen` replaces the now deprecated `shakeLen` key. + * @returns The hash in the format specified. + */ + getHash(format: "HEX", options?: { + outputUpper?: boolean; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "B64", options?: { + b64Pad?: string; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "BYTES", options?: { + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "UINT8ARRAY", options?: { + outputLen?: number; + shakeLen?: number; + }): Uint8Array; + getHash(format: "ARRAYBUFFER", options?: { + outputLen?: number; + shakeLen?: number; + }): ArrayBuffer; + /** + * Sets the HMAC key for an eventual `getHMAC` call. Must be called immediately after jsSHA object instantiation. + * + * @param key The key used to calculate the HMAC + * @param inputFormat The format of key. + * @param options Hashmap of extra input options. + */ + setHMACKey(key: string, inputFormat: "TEXT", options?: { + encoding?: EncodingType; + }): void; + setHMACKey(key: string, inputFormat: "B64" | "HEX" | "BYTES"): void; + setHMACKey(key: ArrayBuffer, inputFormat: "ARRAYBUFFER"): void; + setHMACKey(key: Uint8Array, inputFormat: "UINT8ARRAY"): void; + /** + * Internal function that sets the MAC key. + * + * @param key The packed MAC key to use + */ + protected _setHMACKey(key: packedValue): void; + /** + * Returns the the HMAC in the specified format using the key given by a previous `setHMACKey` call. + * + * @param format The desired output formatting. + * @param options Hashmap of extra outputs options. + * @returns The HMAC in the format specified. + */ + getHMAC(format: "HEX", options?: { + outputUpper?: boolean; + }): string; + getHMAC(format: "B64", options?: { + b64Pad?: string; + }): string; + getHMAC(format: "BYTES"): string; + getHMAC(format: "UINT8ARRAY"): Uint8Array; + getHMAC(format: "ARRAYBUFFER"): ArrayBuffer; + /** + * Internal function that returns the "raw" HMAC + */ + protected _getHMAC(): number[]; +} + +declare class jsSHA extends jsSHABase { + intermediateState: number[]; + variantBlockSize: number; + bigEndianMod: -1 | 1; + outputBinLen: number; + isVariableLen: boolean; + HMACSupported: boolean; + converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue; + roundFunc: (block: number[], H: number[]) => number[]; + finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: number[]) => number[]; + stateCloneFunc: (state: number[]) => number[]; + newStateFunc: (variant: "SHA-1") => number[]; + getMAC: () => number[]; + constructor(variant: "SHA-1", inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + constructor(variant: "SHA-1", inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); +} + +export { jsSHA as default }; diff --git a/dist/sha1.d.ts b/dist/sha1.d.ts index adaa861..79baff5 100644 --- a/dist/sha1.d.ts +++ b/dist/sha1.d.ts @@ -160,4 +160,4 @@ declare class jsSHA extends jsSHABase { constructor(variant: "SHA-1", inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); } -export { jsSHA as default }; +export = jsSHA; diff --git a/dist/sha256.d.cts b/dist/sha256.d.cts new file mode 100644 index 0000000..a096700 --- /dev/null +++ b/dist/sha256.d.cts @@ -0,0 +1,164 @@ +type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE"; +type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY"; +type FormatType = "TEXT" | FormatNoTextType; +type GenericInputType = { + value: string; + format: "TEXT"; + encoding?: EncodingType; +} | { + value: string; + format: "B64" | "HEX" | "BYTES"; +} | { + value: ArrayBuffer; + format: "ARRAYBUFFER"; +} | { + value: Uint8Array; + format: "UINT8ARRAY"; +}; +type FixedLengthOptionsNoEncodingType = { + hmacKey?: GenericInputType; +} | { + numRounds?: number; +}; +type FixedLengthOptionsEncodingType = { + hmacKey?: GenericInputType; + encoding?: EncodingType; +} | { + numRounds?: number; + encoding?: EncodingType; +}; +interface packedValue { + value: number[]; + binLen: number; +} + +declare abstract class jsSHABase { + /** + * @param variant The desired SHA variant. + * @param inputFormat The input format to be used in future `update` calls. + * @param options Hashmap of extra input options. + */ + protected readonly shaVariant: VariantT; + protected readonly inputFormat: FormatType; + protected readonly utfType: EncodingType; + protected readonly numRounds: number; + protected abstract intermediateState: StateT; + protected keyWithIPad: number[]; + protected keyWithOPad: number[]; + protected remainder: number[]; + protected remainderLen: number; + protected updateCalled: boolean; + protected processedLen: number; + protected macKeySet: boolean; + protected abstract readonly variantBlockSize: number; + protected abstract readonly bigEndianMod: -1 | 1; + protected abstract readonly outputBinLen: number; + protected abstract readonly isVariableLen: boolean; + protected abstract readonly HMACSupported: boolean; + protected abstract readonly converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue; + protected abstract readonly roundFunc: (block: number[], H: StateT) => StateT; + protected abstract readonly finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: StateT, outputLen: number) => number[]; + protected abstract readonly stateCloneFunc: (state: StateT) => StateT; + protected abstract readonly newStateFunc: (variant: VariantT) => StateT; + protected abstract readonly getMAC: ((options: { + outputLen: number; + }) => number[]) | null; + protected constructor(variant: VariantT, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + protected constructor(variant: VariantT, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); + /** + * Hashes as many blocks as possible. Stores the rest for either a future update or getHash call. + * + * @param srcString The input to be hashed. + * @returns A reference to the object. + */ + update(srcString: string | ArrayBuffer | Uint8Array): this; + /** + * Returns the desired SHA hash of the input fed in via `update` calls. + * + * @param format The desired output formatting + * @param options Hashmap of output formatting options. `outputLen` must be specified for variable length hashes. + * `outputLen` replaces the now deprecated `shakeLen` key. + * @returns The hash in the format specified. + */ + getHash(format: "HEX", options?: { + outputUpper?: boolean; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "B64", options?: { + b64Pad?: string; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "BYTES", options?: { + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "UINT8ARRAY", options?: { + outputLen?: number; + shakeLen?: number; + }): Uint8Array; + getHash(format: "ARRAYBUFFER", options?: { + outputLen?: number; + shakeLen?: number; + }): ArrayBuffer; + /** + * Sets the HMAC key for an eventual `getHMAC` call. Must be called immediately after jsSHA object instantiation. + * + * @param key The key used to calculate the HMAC + * @param inputFormat The format of key. + * @param options Hashmap of extra input options. + */ + setHMACKey(key: string, inputFormat: "TEXT", options?: { + encoding?: EncodingType; + }): void; + setHMACKey(key: string, inputFormat: "B64" | "HEX" | "BYTES"): void; + setHMACKey(key: ArrayBuffer, inputFormat: "ARRAYBUFFER"): void; + setHMACKey(key: Uint8Array, inputFormat: "UINT8ARRAY"): void; + /** + * Internal function that sets the MAC key. + * + * @param key The packed MAC key to use + */ + protected _setHMACKey(key: packedValue): void; + /** + * Returns the the HMAC in the specified format using the key given by a previous `setHMACKey` call. + * + * @param format The desired output formatting. + * @param options Hashmap of extra outputs options. + * @returns The HMAC in the format specified. + */ + getHMAC(format: "HEX", options?: { + outputUpper?: boolean; + }): string; + getHMAC(format: "B64", options?: { + b64Pad?: string; + }): string; + getHMAC(format: "BYTES"): string; + getHMAC(format: "UINT8ARRAY"): Uint8Array; + getHMAC(format: "ARRAYBUFFER"): ArrayBuffer; + /** + * Internal function that returns the "raw" HMAC + */ + protected _getHMAC(): number[]; +} + +type VariantType = "SHA-224" | "SHA-256"; +declare class jsSHA extends jsSHABase { + intermediateState: number[]; + variantBlockSize: number; + bigEndianMod: -1 | 1; + outputBinLen: number; + isVariableLen: boolean; + HMACSupported: boolean; + converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue; + roundFunc: (block: number[], H: number[]) => number[]; + finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: number[]) => number[]; + stateCloneFunc: (state: number[]) => number[]; + newStateFunc: (variant: VariantType) => number[]; + getMAC: () => number[]; + constructor(variant: VariantType, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + constructor(variant: VariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); +} + +export = jsSHA; diff --git a/dist/sha256.d.mts b/dist/sha256.d.mts new file mode 100644 index 0000000..6f08f42 --- /dev/null +++ b/dist/sha256.d.mts @@ -0,0 +1,164 @@ +type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE"; +type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY"; +type FormatType = "TEXT" | FormatNoTextType; +type GenericInputType = { + value: string; + format: "TEXT"; + encoding?: EncodingType; +} | { + value: string; + format: "B64" | "HEX" | "BYTES"; +} | { + value: ArrayBuffer; + format: "ARRAYBUFFER"; +} | { + value: Uint8Array; + format: "UINT8ARRAY"; +}; +type FixedLengthOptionsNoEncodingType = { + hmacKey?: GenericInputType; +} | { + numRounds?: number; +}; +type FixedLengthOptionsEncodingType = { + hmacKey?: GenericInputType; + encoding?: EncodingType; +} | { + numRounds?: number; + encoding?: EncodingType; +}; +interface packedValue { + value: number[]; + binLen: number; +} + +declare abstract class jsSHABase { + /** + * @param variant The desired SHA variant. + * @param inputFormat The input format to be used in future `update` calls. + * @param options Hashmap of extra input options. + */ + protected readonly shaVariant: VariantT; + protected readonly inputFormat: FormatType; + protected readonly utfType: EncodingType; + protected readonly numRounds: number; + protected abstract intermediateState: StateT; + protected keyWithIPad: number[]; + protected keyWithOPad: number[]; + protected remainder: number[]; + protected remainderLen: number; + protected updateCalled: boolean; + protected processedLen: number; + protected macKeySet: boolean; + protected abstract readonly variantBlockSize: number; + protected abstract readonly bigEndianMod: -1 | 1; + protected abstract readonly outputBinLen: number; + protected abstract readonly isVariableLen: boolean; + protected abstract readonly HMACSupported: boolean; + protected abstract readonly converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue; + protected abstract readonly roundFunc: (block: number[], H: StateT) => StateT; + protected abstract readonly finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: StateT, outputLen: number) => number[]; + protected abstract readonly stateCloneFunc: (state: StateT) => StateT; + protected abstract readonly newStateFunc: (variant: VariantT) => StateT; + protected abstract readonly getMAC: ((options: { + outputLen: number; + }) => number[]) | null; + protected constructor(variant: VariantT, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + protected constructor(variant: VariantT, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); + /** + * Hashes as many blocks as possible. Stores the rest for either a future update or getHash call. + * + * @param srcString The input to be hashed. + * @returns A reference to the object. + */ + update(srcString: string | ArrayBuffer | Uint8Array): this; + /** + * Returns the desired SHA hash of the input fed in via `update` calls. + * + * @param format The desired output formatting + * @param options Hashmap of output formatting options. `outputLen` must be specified for variable length hashes. + * `outputLen` replaces the now deprecated `shakeLen` key. + * @returns The hash in the format specified. + */ + getHash(format: "HEX", options?: { + outputUpper?: boolean; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "B64", options?: { + b64Pad?: string; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "BYTES", options?: { + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "UINT8ARRAY", options?: { + outputLen?: number; + shakeLen?: number; + }): Uint8Array; + getHash(format: "ARRAYBUFFER", options?: { + outputLen?: number; + shakeLen?: number; + }): ArrayBuffer; + /** + * Sets the HMAC key for an eventual `getHMAC` call. Must be called immediately after jsSHA object instantiation. + * + * @param key The key used to calculate the HMAC + * @param inputFormat The format of key. + * @param options Hashmap of extra input options. + */ + setHMACKey(key: string, inputFormat: "TEXT", options?: { + encoding?: EncodingType; + }): void; + setHMACKey(key: string, inputFormat: "B64" | "HEX" | "BYTES"): void; + setHMACKey(key: ArrayBuffer, inputFormat: "ARRAYBUFFER"): void; + setHMACKey(key: Uint8Array, inputFormat: "UINT8ARRAY"): void; + /** + * Internal function that sets the MAC key. + * + * @param key The packed MAC key to use + */ + protected _setHMACKey(key: packedValue): void; + /** + * Returns the the HMAC in the specified format using the key given by a previous `setHMACKey` call. + * + * @param format The desired output formatting. + * @param options Hashmap of extra outputs options. + * @returns The HMAC in the format specified. + */ + getHMAC(format: "HEX", options?: { + outputUpper?: boolean; + }): string; + getHMAC(format: "B64", options?: { + b64Pad?: string; + }): string; + getHMAC(format: "BYTES"): string; + getHMAC(format: "UINT8ARRAY"): Uint8Array; + getHMAC(format: "ARRAYBUFFER"): ArrayBuffer; + /** + * Internal function that returns the "raw" HMAC + */ + protected _getHMAC(): number[]; +} + +type VariantType = "SHA-224" | "SHA-256"; +declare class jsSHA extends jsSHABase { + intermediateState: number[]; + variantBlockSize: number; + bigEndianMod: -1 | 1; + outputBinLen: number; + isVariableLen: boolean; + HMACSupported: boolean; + converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue; + roundFunc: (block: number[], H: number[]) => number[]; + finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: number[]) => number[]; + stateCloneFunc: (state: number[]) => number[]; + newStateFunc: (variant: VariantType) => number[]; + getMAC: () => number[]; + constructor(variant: VariantType, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + constructor(variant: VariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); +} + +export { jsSHA as default }; diff --git a/dist/sha256.d.ts b/dist/sha256.d.ts index 6f08f42..a096700 100644 --- a/dist/sha256.d.ts +++ b/dist/sha256.d.ts @@ -161,4 +161,4 @@ declare class jsSHA extends jsSHABase { constructor(variant: VariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); } -export { jsSHA as default }; +export = jsSHA; diff --git a/dist/sha3.d.cts b/dist/sha3.d.cts new file mode 100644 index 0000000..9103f8a --- /dev/null +++ b/dist/sha3.d.cts @@ -0,0 +1,229 @@ +type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE"; +type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY"; +type FormatType = "TEXT" | FormatNoTextType; +type GenericInputType = { + value: string; + format: "TEXT"; + encoding?: EncodingType; +} | { + value: string; + format: "B64" | "HEX" | "BYTES"; +} | { + value: ArrayBuffer; + format: "ARRAYBUFFER"; +} | { + value: Uint8Array; + format: "UINT8ARRAY"; +}; +type FixedLengthOptionsNoEncodingType = { + hmacKey?: GenericInputType; +} | { + numRounds?: number; +}; +type FixedLengthOptionsEncodingType = { + hmacKey?: GenericInputType; + encoding?: EncodingType; +} | { + numRounds?: number; + encoding?: EncodingType; +}; +interface packedValue { + value: number[]; + binLen: number; +} +interface SHAKEOptionsNoEncodingType { + numRounds?: number; +} +interface SHAKEOptionsEncodingType extends SHAKEOptionsNoEncodingType { + encoding?: EncodingType; +} +interface CSHAKEOptionsNoEncodingType { + customization?: GenericInputType; + funcName?: GenericInputType; +} +interface CSHAKEOptionsEncodingType extends CSHAKEOptionsNoEncodingType { + encoding?: EncodingType; +} +interface KMACOptionsNoEncodingType { + kmacKey: GenericInputType; + customization?: GenericInputType; +} +interface KMACOptionsEncodingType extends KMACOptionsNoEncodingType { + encoding?: EncodingType; +} + +declare abstract class jsSHABase { + /** + * @param variant The desired SHA variant. + * @param inputFormat The input format to be used in future `update` calls. + * @param options Hashmap of extra input options. + */ + protected readonly shaVariant: VariantT; + protected readonly inputFormat: FormatType; + protected readonly utfType: EncodingType; + protected readonly numRounds: number; + protected abstract intermediateState: StateT; + protected keyWithIPad: number[]; + protected keyWithOPad: number[]; + protected remainder: number[]; + protected remainderLen: number; + protected updateCalled: boolean; + protected processedLen: number; + protected macKeySet: boolean; + protected abstract readonly variantBlockSize: number; + protected abstract readonly bigEndianMod: -1 | 1; + protected abstract readonly outputBinLen: number; + protected abstract readonly isVariableLen: boolean; + protected abstract readonly HMACSupported: boolean; + protected abstract readonly converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue; + protected abstract readonly roundFunc: (block: number[], H: StateT) => StateT; + protected abstract readonly finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: StateT, outputLen: number) => number[]; + protected abstract readonly stateCloneFunc: (state: StateT) => StateT; + protected abstract readonly newStateFunc: (variant: VariantT) => StateT; + protected abstract readonly getMAC: ((options: { + outputLen: number; + }) => number[]) | null; + protected constructor(variant: VariantT, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + protected constructor(variant: VariantT, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); + /** + * Hashes as many blocks as possible. Stores the rest for either a future update or getHash call. + * + * @param srcString The input to be hashed. + * @returns A reference to the object. + */ + update(srcString: string | ArrayBuffer | Uint8Array): this; + /** + * Returns the desired SHA hash of the input fed in via `update` calls. + * + * @param format The desired output formatting + * @param options Hashmap of output formatting options. `outputLen` must be specified for variable length hashes. + * `outputLen` replaces the now deprecated `shakeLen` key. + * @returns The hash in the format specified. + */ + getHash(format: "HEX", options?: { + outputUpper?: boolean; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "B64", options?: { + b64Pad?: string; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "BYTES", options?: { + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "UINT8ARRAY", options?: { + outputLen?: number; + shakeLen?: number; + }): Uint8Array; + getHash(format: "ARRAYBUFFER", options?: { + outputLen?: number; + shakeLen?: number; + }): ArrayBuffer; + /** + * Sets the HMAC key for an eventual `getHMAC` call. Must be called immediately after jsSHA object instantiation. + * + * @param key The key used to calculate the HMAC + * @param inputFormat The format of key. + * @param options Hashmap of extra input options. + */ + setHMACKey(key: string, inputFormat: "TEXT", options?: { + encoding?: EncodingType; + }): void; + setHMACKey(key: string, inputFormat: "B64" | "HEX" | "BYTES"): void; + setHMACKey(key: ArrayBuffer, inputFormat: "ARRAYBUFFER"): void; + setHMACKey(key: Uint8Array, inputFormat: "UINT8ARRAY"): void; + /** + * Internal function that sets the MAC key. + * + * @param key The packed MAC key to use + */ + protected _setHMACKey(key: packedValue): void; + /** + * Returns the the HMAC in the specified format using the key given by a previous `setHMACKey` call. + * + * @param format The desired output formatting. + * @param options Hashmap of extra outputs options. + * @returns The HMAC in the format specified. + */ + getHMAC(format: "HEX", options?: { + outputUpper?: boolean; + }): string; + getHMAC(format: "B64", options?: { + b64Pad?: string; + }): string; + getHMAC(format: "BYTES"): string; + getHMAC(format: "UINT8ARRAY"): Uint8Array; + getHMAC(format: "ARRAYBUFFER"): ArrayBuffer; + /** + * Internal function that returns the "raw" HMAC + */ + protected _getHMAC(): number[]; +} + +/** + * Int_64 is a object for 2 32-bit numbers emulating a 64-bit number. + */ +declare class Int_64 { + /** + * @param msint_32 The most significant 32-bits of a 64-bit number. + * @param lsint_32 The least significant 32-bits of a 64-bit number. + */ + readonly highOrder: number; + readonly lowOrder: number; + constructor(msint_32: number, lsint_32: number); +} + +type FixedLengthVariantType = "SHA3-224" | "SHA3-256" | "SHA3-384" | "SHA3-512" | "SHAKE128" | "SHAKE256"; +type VariantType = FixedLengthVariantType | "SHAKE128" | "SHAKE256" | "CSHAKE128" | "CSHAKE256" | "KMAC128" | "KMAC256"; +declare class jsSHA extends jsSHABase { + intermediateState: Int_64[][]; + variantBlockSize: number; + bigEndianMod: -1 | 1; + outputBinLen: number; + isVariableLen: boolean; + HMACSupported: boolean; + converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue; + roundFunc: (block: number[], H: Int_64[][]) => Int_64[][]; + finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: Int_64[][], outputLen: number) => number[]; + stateCloneFunc: (state: Int_64[][]) => Int_64[][]; + newStateFunc: (variant: VariantType) => Int_64[][]; + getMAC: ((options: { + outputLen: number; + }) => number[]) | null; + constructor(variant: FixedLengthVariantType, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + constructor(variant: FixedLengthVariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); + constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: "TEXT", options?: SHAKEOptionsEncodingType); + constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: FormatNoTextType, options?: SHAKEOptionsNoEncodingType); + constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: "TEXT", options?: CSHAKEOptionsEncodingType); + constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: FormatNoTextType, options?: CSHAKEOptionsNoEncodingType); + constructor(variant: "KMAC128" | "KMAC256", inputFormat: "TEXT", options: KMACOptionsEncodingType); + constructor(variant: "KMAC128" | "KMAC256", inputFormat: FormatNoTextType, options: KMACOptionsNoEncodingType); + /** + * Initialize CSHAKE variants. + * + * @param options Options containing CSHAKE params. + * @param funcNameOverride Overrides any "funcName" present in `options` (used with KMAC) + * @returns The delimiter to be used + */ + protected _initializeCSHAKE(options?: CSHAKEOptionsNoEncodingType, funcNameOverride?: packedValue): number; + /** + * Initialize KMAC variants. + * + * @param options Options containing KMAC params. + */ + protected _initializeKMAC(options: KMACOptionsNoEncodingType): void; + /** + * Returns the the KMAC in the specified format. + * + * @param options Hashmap of extra outputs options. `outputLen` must be specified. + * @returns The KMAC in the format specified. + */ + protected _getKMAC(options: { + outputLen: number; + }): number[]; +} + +export = jsSHA; diff --git a/dist/sha3.d.mts b/dist/sha3.d.mts new file mode 100644 index 0000000..ad62bc2 --- /dev/null +++ b/dist/sha3.d.mts @@ -0,0 +1,229 @@ +type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE"; +type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY"; +type FormatType = "TEXT" | FormatNoTextType; +type GenericInputType = { + value: string; + format: "TEXT"; + encoding?: EncodingType; +} | { + value: string; + format: "B64" | "HEX" | "BYTES"; +} | { + value: ArrayBuffer; + format: "ARRAYBUFFER"; +} | { + value: Uint8Array; + format: "UINT8ARRAY"; +}; +type FixedLengthOptionsNoEncodingType = { + hmacKey?: GenericInputType; +} | { + numRounds?: number; +}; +type FixedLengthOptionsEncodingType = { + hmacKey?: GenericInputType; + encoding?: EncodingType; +} | { + numRounds?: number; + encoding?: EncodingType; +}; +interface packedValue { + value: number[]; + binLen: number; +} +interface SHAKEOptionsNoEncodingType { + numRounds?: number; +} +interface SHAKEOptionsEncodingType extends SHAKEOptionsNoEncodingType { + encoding?: EncodingType; +} +interface CSHAKEOptionsNoEncodingType { + customization?: GenericInputType; + funcName?: GenericInputType; +} +interface CSHAKEOptionsEncodingType extends CSHAKEOptionsNoEncodingType { + encoding?: EncodingType; +} +interface KMACOptionsNoEncodingType { + kmacKey: GenericInputType; + customization?: GenericInputType; +} +interface KMACOptionsEncodingType extends KMACOptionsNoEncodingType { + encoding?: EncodingType; +} + +declare abstract class jsSHABase { + /** + * @param variant The desired SHA variant. + * @param inputFormat The input format to be used in future `update` calls. + * @param options Hashmap of extra input options. + */ + protected readonly shaVariant: VariantT; + protected readonly inputFormat: FormatType; + protected readonly utfType: EncodingType; + protected readonly numRounds: number; + protected abstract intermediateState: StateT; + protected keyWithIPad: number[]; + protected keyWithOPad: number[]; + protected remainder: number[]; + protected remainderLen: number; + protected updateCalled: boolean; + protected processedLen: number; + protected macKeySet: boolean; + protected abstract readonly variantBlockSize: number; + protected abstract readonly bigEndianMod: -1 | 1; + protected abstract readonly outputBinLen: number; + protected abstract readonly isVariableLen: boolean; + protected abstract readonly HMACSupported: boolean; + protected abstract readonly converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue; + protected abstract readonly roundFunc: (block: number[], H: StateT) => StateT; + protected abstract readonly finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: StateT, outputLen: number) => number[]; + protected abstract readonly stateCloneFunc: (state: StateT) => StateT; + protected abstract readonly newStateFunc: (variant: VariantT) => StateT; + protected abstract readonly getMAC: ((options: { + outputLen: number; + }) => number[]) | null; + protected constructor(variant: VariantT, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + protected constructor(variant: VariantT, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); + /** + * Hashes as many blocks as possible. Stores the rest for either a future update or getHash call. + * + * @param srcString The input to be hashed. + * @returns A reference to the object. + */ + update(srcString: string | ArrayBuffer | Uint8Array): this; + /** + * Returns the desired SHA hash of the input fed in via `update` calls. + * + * @param format The desired output formatting + * @param options Hashmap of output formatting options. `outputLen` must be specified for variable length hashes. + * `outputLen` replaces the now deprecated `shakeLen` key. + * @returns The hash in the format specified. + */ + getHash(format: "HEX", options?: { + outputUpper?: boolean; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "B64", options?: { + b64Pad?: string; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "BYTES", options?: { + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "UINT8ARRAY", options?: { + outputLen?: number; + shakeLen?: number; + }): Uint8Array; + getHash(format: "ARRAYBUFFER", options?: { + outputLen?: number; + shakeLen?: number; + }): ArrayBuffer; + /** + * Sets the HMAC key for an eventual `getHMAC` call. Must be called immediately after jsSHA object instantiation. + * + * @param key The key used to calculate the HMAC + * @param inputFormat The format of key. + * @param options Hashmap of extra input options. + */ + setHMACKey(key: string, inputFormat: "TEXT", options?: { + encoding?: EncodingType; + }): void; + setHMACKey(key: string, inputFormat: "B64" | "HEX" | "BYTES"): void; + setHMACKey(key: ArrayBuffer, inputFormat: "ARRAYBUFFER"): void; + setHMACKey(key: Uint8Array, inputFormat: "UINT8ARRAY"): void; + /** + * Internal function that sets the MAC key. + * + * @param key The packed MAC key to use + */ + protected _setHMACKey(key: packedValue): void; + /** + * Returns the the HMAC in the specified format using the key given by a previous `setHMACKey` call. + * + * @param format The desired output formatting. + * @param options Hashmap of extra outputs options. + * @returns The HMAC in the format specified. + */ + getHMAC(format: "HEX", options?: { + outputUpper?: boolean; + }): string; + getHMAC(format: "B64", options?: { + b64Pad?: string; + }): string; + getHMAC(format: "BYTES"): string; + getHMAC(format: "UINT8ARRAY"): Uint8Array; + getHMAC(format: "ARRAYBUFFER"): ArrayBuffer; + /** + * Internal function that returns the "raw" HMAC + */ + protected _getHMAC(): number[]; +} + +/** + * Int_64 is a object for 2 32-bit numbers emulating a 64-bit number. + */ +declare class Int_64 { + /** + * @param msint_32 The most significant 32-bits of a 64-bit number. + * @param lsint_32 The least significant 32-bits of a 64-bit number. + */ + readonly highOrder: number; + readonly lowOrder: number; + constructor(msint_32: number, lsint_32: number); +} + +type FixedLengthVariantType = "SHA3-224" | "SHA3-256" | "SHA3-384" | "SHA3-512" | "SHAKE128" | "SHAKE256"; +type VariantType = FixedLengthVariantType | "SHAKE128" | "SHAKE256" | "CSHAKE128" | "CSHAKE256" | "KMAC128" | "KMAC256"; +declare class jsSHA extends jsSHABase { + intermediateState: Int_64[][]; + variantBlockSize: number; + bigEndianMod: -1 | 1; + outputBinLen: number; + isVariableLen: boolean; + HMACSupported: boolean; + converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue; + roundFunc: (block: number[], H: Int_64[][]) => Int_64[][]; + finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: Int_64[][], outputLen: number) => number[]; + stateCloneFunc: (state: Int_64[][]) => Int_64[][]; + newStateFunc: (variant: VariantType) => Int_64[][]; + getMAC: ((options: { + outputLen: number; + }) => number[]) | null; + constructor(variant: FixedLengthVariantType, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + constructor(variant: FixedLengthVariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); + constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: "TEXT", options?: SHAKEOptionsEncodingType); + constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: FormatNoTextType, options?: SHAKEOptionsNoEncodingType); + constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: "TEXT", options?: CSHAKEOptionsEncodingType); + constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: FormatNoTextType, options?: CSHAKEOptionsNoEncodingType); + constructor(variant: "KMAC128" | "KMAC256", inputFormat: "TEXT", options: KMACOptionsEncodingType); + constructor(variant: "KMAC128" | "KMAC256", inputFormat: FormatNoTextType, options: KMACOptionsNoEncodingType); + /** + * Initialize CSHAKE variants. + * + * @param options Options containing CSHAKE params. + * @param funcNameOverride Overrides any "funcName" present in `options` (used with KMAC) + * @returns The delimiter to be used + */ + protected _initializeCSHAKE(options?: CSHAKEOptionsNoEncodingType, funcNameOverride?: packedValue): number; + /** + * Initialize KMAC variants. + * + * @param options Options containing KMAC params. + */ + protected _initializeKMAC(options: KMACOptionsNoEncodingType): void; + /** + * Returns the the KMAC in the specified format. + * + * @param options Hashmap of extra outputs options. `outputLen` must be specified. + * @returns The KMAC in the format specified. + */ + protected _getKMAC(options: { + outputLen: number; + }): number[]; +} + +export { jsSHA as default }; diff --git a/dist/sha3.d.ts b/dist/sha3.d.ts index ad62bc2..9103f8a 100644 --- a/dist/sha3.d.ts +++ b/dist/sha3.d.ts @@ -226,4 +226,4 @@ declare class jsSHA extends jsSHABase { }): number[]; } -export { jsSHA as default }; +export = jsSHA; diff --git a/dist/sha512.d.cts b/dist/sha512.d.cts new file mode 100644 index 0000000..43a5ed9 --- /dev/null +++ b/dist/sha512.d.cts @@ -0,0 +1,177 @@ +type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE"; +type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY"; +type FormatType = "TEXT" | FormatNoTextType; +type GenericInputType = { + value: string; + format: "TEXT"; + encoding?: EncodingType; +} | { + value: string; + format: "B64" | "HEX" | "BYTES"; +} | { + value: ArrayBuffer; + format: "ARRAYBUFFER"; +} | { + value: Uint8Array; + format: "UINT8ARRAY"; +}; +type FixedLengthOptionsNoEncodingType = { + hmacKey?: GenericInputType; +} | { + numRounds?: number; +}; +type FixedLengthOptionsEncodingType = { + hmacKey?: GenericInputType; + encoding?: EncodingType; +} | { + numRounds?: number; + encoding?: EncodingType; +}; +interface packedValue { + value: number[]; + binLen: number; +} + +declare abstract class jsSHABase { + /** + * @param variant The desired SHA variant. + * @param inputFormat The input format to be used in future `update` calls. + * @param options Hashmap of extra input options. + */ + protected readonly shaVariant: VariantT; + protected readonly inputFormat: FormatType; + protected readonly utfType: EncodingType; + protected readonly numRounds: number; + protected abstract intermediateState: StateT; + protected keyWithIPad: number[]; + protected keyWithOPad: number[]; + protected remainder: number[]; + protected remainderLen: number; + protected updateCalled: boolean; + protected processedLen: number; + protected macKeySet: boolean; + protected abstract readonly variantBlockSize: number; + protected abstract readonly bigEndianMod: -1 | 1; + protected abstract readonly outputBinLen: number; + protected abstract readonly isVariableLen: boolean; + protected abstract readonly HMACSupported: boolean; + protected abstract readonly converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue; + protected abstract readonly roundFunc: (block: number[], H: StateT) => StateT; + protected abstract readonly finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: StateT, outputLen: number) => number[]; + protected abstract readonly stateCloneFunc: (state: StateT) => StateT; + protected abstract readonly newStateFunc: (variant: VariantT) => StateT; + protected abstract readonly getMAC: ((options: { + outputLen: number; + }) => number[]) | null; + protected constructor(variant: VariantT, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + protected constructor(variant: VariantT, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); + /** + * Hashes as many blocks as possible. Stores the rest for either a future update or getHash call. + * + * @param srcString The input to be hashed. + * @returns A reference to the object. + */ + update(srcString: string | ArrayBuffer | Uint8Array): this; + /** + * Returns the desired SHA hash of the input fed in via `update` calls. + * + * @param format The desired output formatting + * @param options Hashmap of output formatting options. `outputLen` must be specified for variable length hashes. + * `outputLen` replaces the now deprecated `shakeLen` key. + * @returns The hash in the format specified. + */ + getHash(format: "HEX", options?: { + outputUpper?: boolean; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "B64", options?: { + b64Pad?: string; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "BYTES", options?: { + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "UINT8ARRAY", options?: { + outputLen?: number; + shakeLen?: number; + }): Uint8Array; + getHash(format: "ARRAYBUFFER", options?: { + outputLen?: number; + shakeLen?: number; + }): ArrayBuffer; + /** + * Sets the HMAC key for an eventual `getHMAC` call. Must be called immediately after jsSHA object instantiation. + * + * @param key The key used to calculate the HMAC + * @param inputFormat The format of key. + * @param options Hashmap of extra input options. + */ + setHMACKey(key: string, inputFormat: "TEXT", options?: { + encoding?: EncodingType; + }): void; + setHMACKey(key: string, inputFormat: "B64" | "HEX" | "BYTES"): void; + setHMACKey(key: ArrayBuffer, inputFormat: "ARRAYBUFFER"): void; + setHMACKey(key: Uint8Array, inputFormat: "UINT8ARRAY"): void; + /** + * Internal function that sets the MAC key. + * + * @param key The packed MAC key to use + */ + protected _setHMACKey(key: packedValue): void; + /** + * Returns the the HMAC in the specified format using the key given by a previous `setHMACKey` call. + * + * @param format The desired output formatting. + * @param options Hashmap of extra outputs options. + * @returns The HMAC in the format specified. + */ + getHMAC(format: "HEX", options?: { + outputUpper?: boolean; + }): string; + getHMAC(format: "B64", options?: { + b64Pad?: string; + }): string; + getHMAC(format: "BYTES"): string; + getHMAC(format: "UINT8ARRAY"): Uint8Array; + getHMAC(format: "ARRAYBUFFER"): ArrayBuffer; + /** + * Internal function that returns the "raw" HMAC + */ + protected _getHMAC(): number[]; +} + +/** + * Int_64 is a object for 2 32-bit numbers emulating a 64-bit number. + */ +declare class Int_64 { + /** + * @param msint_32 The most significant 32-bits of a 64-bit number. + * @param lsint_32 The least significant 32-bits of a 64-bit number. + */ + readonly highOrder: number; + readonly lowOrder: number; + constructor(msint_32: number, lsint_32: number); +} + +type VariantType = "SHA-384" | "SHA-512"; +declare class jsSHA extends jsSHABase { + intermediateState: Int_64[]; + variantBlockSize: number; + bigEndianMod: -1 | 1; + outputBinLen: number; + isVariableLen: boolean; + HMACSupported: boolean; + converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue; + roundFunc: (block: number[], H: Int_64[]) => Int_64[]; + finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: Int_64[]) => number[]; + stateCloneFunc: (state: Int_64[]) => Int_64[]; + newStateFunc: (variant: VariantType) => Int_64[]; + getMAC: () => number[]; + constructor(variant: VariantType, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + constructor(variant: VariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); +} + +export = jsSHA; diff --git a/dist/sha512.d.mts b/dist/sha512.d.mts new file mode 100644 index 0000000..e05e25e --- /dev/null +++ b/dist/sha512.d.mts @@ -0,0 +1,177 @@ +type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE"; +type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY"; +type FormatType = "TEXT" | FormatNoTextType; +type GenericInputType = { + value: string; + format: "TEXT"; + encoding?: EncodingType; +} | { + value: string; + format: "B64" | "HEX" | "BYTES"; +} | { + value: ArrayBuffer; + format: "ARRAYBUFFER"; +} | { + value: Uint8Array; + format: "UINT8ARRAY"; +}; +type FixedLengthOptionsNoEncodingType = { + hmacKey?: GenericInputType; +} | { + numRounds?: number; +}; +type FixedLengthOptionsEncodingType = { + hmacKey?: GenericInputType; + encoding?: EncodingType; +} | { + numRounds?: number; + encoding?: EncodingType; +}; +interface packedValue { + value: number[]; + binLen: number; +} + +declare abstract class jsSHABase { + /** + * @param variant The desired SHA variant. + * @param inputFormat The input format to be used in future `update` calls. + * @param options Hashmap of extra input options. + */ + protected readonly shaVariant: VariantT; + protected readonly inputFormat: FormatType; + protected readonly utfType: EncodingType; + protected readonly numRounds: number; + protected abstract intermediateState: StateT; + protected keyWithIPad: number[]; + protected keyWithOPad: number[]; + protected remainder: number[]; + protected remainderLen: number; + protected updateCalled: boolean; + protected processedLen: number; + protected macKeySet: boolean; + protected abstract readonly variantBlockSize: number; + protected abstract readonly bigEndianMod: -1 | 1; + protected abstract readonly outputBinLen: number; + protected abstract readonly isVariableLen: boolean; + protected abstract readonly HMACSupported: boolean; + protected abstract readonly converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue; + protected abstract readonly roundFunc: (block: number[], H: StateT) => StateT; + protected abstract readonly finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: StateT, outputLen: number) => number[]; + protected abstract readonly stateCloneFunc: (state: StateT) => StateT; + protected abstract readonly newStateFunc: (variant: VariantT) => StateT; + protected abstract readonly getMAC: ((options: { + outputLen: number; + }) => number[]) | null; + protected constructor(variant: VariantT, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + protected constructor(variant: VariantT, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); + /** + * Hashes as many blocks as possible. Stores the rest for either a future update or getHash call. + * + * @param srcString The input to be hashed. + * @returns A reference to the object. + */ + update(srcString: string | ArrayBuffer | Uint8Array): this; + /** + * Returns the desired SHA hash of the input fed in via `update` calls. + * + * @param format The desired output formatting + * @param options Hashmap of output formatting options. `outputLen` must be specified for variable length hashes. + * `outputLen` replaces the now deprecated `shakeLen` key. + * @returns The hash in the format specified. + */ + getHash(format: "HEX", options?: { + outputUpper?: boolean; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "B64", options?: { + b64Pad?: string; + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "BYTES", options?: { + outputLen?: number; + shakeLen?: number; + }): string; + getHash(format: "UINT8ARRAY", options?: { + outputLen?: number; + shakeLen?: number; + }): Uint8Array; + getHash(format: "ARRAYBUFFER", options?: { + outputLen?: number; + shakeLen?: number; + }): ArrayBuffer; + /** + * Sets the HMAC key for an eventual `getHMAC` call. Must be called immediately after jsSHA object instantiation. + * + * @param key The key used to calculate the HMAC + * @param inputFormat The format of key. + * @param options Hashmap of extra input options. + */ + setHMACKey(key: string, inputFormat: "TEXT", options?: { + encoding?: EncodingType; + }): void; + setHMACKey(key: string, inputFormat: "B64" | "HEX" | "BYTES"): void; + setHMACKey(key: ArrayBuffer, inputFormat: "ARRAYBUFFER"): void; + setHMACKey(key: Uint8Array, inputFormat: "UINT8ARRAY"): void; + /** + * Internal function that sets the MAC key. + * + * @param key The packed MAC key to use + */ + protected _setHMACKey(key: packedValue): void; + /** + * Returns the the HMAC in the specified format using the key given by a previous `setHMACKey` call. + * + * @param format The desired output formatting. + * @param options Hashmap of extra outputs options. + * @returns The HMAC in the format specified. + */ + getHMAC(format: "HEX", options?: { + outputUpper?: boolean; + }): string; + getHMAC(format: "B64", options?: { + b64Pad?: string; + }): string; + getHMAC(format: "BYTES"): string; + getHMAC(format: "UINT8ARRAY"): Uint8Array; + getHMAC(format: "ARRAYBUFFER"): ArrayBuffer; + /** + * Internal function that returns the "raw" HMAC + */ + protected _getHMAC(): number[]; +} + +/** + * Int_64 is a object for 2 32-bit numbers emulating a 64-bit number. + */ +declare class Int_64 { + /** + * @param msint_32 The most significant 32-bits of a 64-bit number. + * @param lsint_32 The least significant 32-bits of a 64-bit number. + */ + readonly highOrder: number; + readonly lowOrder: number; + constructor(msint_32: number, lsint_32: number); +} + +type VariantType = "SHA-384" | "SHA-512"; +declare class jsSHA extends jsSHABase { + intermediateState: Int_64[]; + variantBlockSize: number; + bigEndianMod: -1 | 1; + outputBinLen: number; + isVariableLen: boolean; + HMACSupported: boolean; + converterFunc: (input: any, existingBin: number[], existingBinLen: number) => packedValue; + roundFunc: (block: number[], H: Int_64[]) => Int_64[]; + finalizeFunc: (remainder: number[], remainderBinLen: number, processedBinLen: number, H: Int_64[]) => number[]; + stateCloneFunc: (state: Int_64[]) => Int_64[]; + newStateFunc: (variant: VariantType) => Int_64[]; + getMAC: () => number[]; + constructor(variant: VariantType, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType); + constructor(variant: VariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); +} + +export { jsSHA as default }; diff --git a/dist/sha512.d.ts b/dist/sha512.d.ts index e05e25e..43a5ed9 100644 --- a/dist/sha512.d.ts +++ b/dist/sha512.d.ts @@ -174,4 +174,4 @@ declare class jsSHA extends jsSHABase { constructor(variant: VariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType); } -export { jsSHA as default }; +export = jsSHA; diff --git a/package.json b/package.json index 4e53bd0..c856353 100644 --- a/package.json +++ b/package.json @@ -5,49 +5,94 @@ "main": "./dist/sha.js", "exports": { ".": { - "types": "./dist/sha.d.ts", - "import": "./dist/sha.mjs", - "require": "./dist/sha.js" + "import": { + "types": "./dist/sha.d.mts", + "default": "./dist/sha.mjs" + }, + "require": { + "types": "./dist/sha.d.cts", + "default": "./dist/sha.js" + } }, "./sha1": { - "types": "./dist/sha1.d.ts", - "import": "./dist/sha1.mjs", - "require": "./dist/sha1.js" + "import": { + "types": "./dist/sha1.d.mts", + "default": "./dist/sha1.mjs" + }, + "require": { + "types": "./dist/sha1.d.cts", + "default": "./dist/sha1.js" + } }, "./dist/sha1": { - "types": "./dist/sha1.d.ts", - "import": "./dist/sha1.mjs", - "require": "./dist/sha1.js" + "import": { + "types": "./dist/sha1.d.mts", + "default": "./dist/sha1.mjs" + }, + "require": { + "types": "./dist/sha1.d.cts", + "default": "./dist/sha1.js" + } }, "./sha256": { - "types": "./dist/sha256.d.ts", - "import": "./dist/sha256.mjs", - "require": "./dist/sha256.js" + "import": { + "types": "./dist/sha256.d.mts", + "default": "./dist/sha256.mjs" + }, + "require": { + "types": "./dist/sha256.d.cts", + "default": "./dist/sha256.js" + } }, "./dist/sha256": { - "types": "./dist/sha256.d.ts", - "import": "./dist/sha256.mjs", - "require": "./dist/sha256.js" + "import": { + "types": "./dist/sha256.d.mts", + "default": "./dist/sha256.mjs" + }, + "require": { + "types": "./dist/sha256.d.cts", + "default": "./dist/sha256.js" + } }, "./sha512": { - "types": "./dist/sha512.d.ts", - "import": "./dist/sha512.mjs", - "require": "./dist/sha512.js" + "import": { + "types": "./dist/sha512.d.mts", + "default": "./dist/sha512.mjs" + }, + "require": { + "types": "./dist/sha512.d.cts", + "default": "./dist/sha512.js" + } }, "./dist/sha512": { - "types": "./dist/sha512.d.ts", - "import": "./dist/sha512.mjs", - "require": "./dist/sha512.js" + "import": { + "types": "./dist/sha512.d.mts", + "default": "./dist/sha512.mjs" + }, + "require": { + "types": "./dist/sha512.d.cts", + "default": "./dist/sha512.js" + } }, "./sha3": { - "types": "./dist/sha3.d.ts", - "import": "./dist/sha3.mjs", - "require": "./dist/sha3.js" + "import": { + "types": "./dist/sha3.d.mts", + "default": "./dist/sha3.mjs" + }, + "require": { + "types": "./dist/sha3.d.cts", + "default": "./dist/sha3.js" + } }, "./dist/sha3": { - "types": "./dist/sha3.d.ts", - "import": "./dist/sha3.mjs", - "require": "./dist/sha3.js" + "import": { + "types": "./dist/sha3.d.mts", + "default": "./dist/sha3.mjs" + }, + "require": { + "types": "./dist/sha3.d.cts", + "default": "./dist/sha3.js" + } }, "./package.json": "./package.json" }, @@ -134,7 +179,7 @@ "typescript-eslint": "^8.61.0" }, "scripts": { - "build": "rollup -c", + "build": "rollup -c && node scripts/generate-dual-types.mjs", "test": "nyc --reporter=html --reporter=text mocha test/src/*.ts", "test_dist": "mocha test/dist/ && karma start karma.conf.js --file-variant sha && karma start karma.conf.js --file-variant sha1 && karma start karma.conf.js --file-variant sha256 && karma start karma.conf.js --file-variant sha512 && karma start karma.conf.js --file-variant sha3", "prepare": "husky" @@ -157,6 +202,7 @@ "*.js", "coverage", "test", + "scripts", "rollup.config.mjs" ], "reporter": [ diff --git a/scripts/generate-dual-types.mjs b/scripts/generate-dual-types.mjs new file mode 100644 index 0000000..b2c82b1 --- /dev/null +++ b/scripts/generate-dual-types.mjs @@ -0,0 +1,31 @@ +import fs from "fs"; + +const variants = ["sha", "sha1", "sha256", "sha512", "sha3"]; + +for (const variant of variants) { + const srcPath = `dist/${variant}.d.ts`; + const esmContent = fs.readFileSync(srcPath, { encoding: "utf8" }); + + if (!esmContent.includes("export { jsSHA as default };")) { + throw new Error( + `${srcPath} did not contain the expected "export { jsSHA as default };" line - ` + + "the dts bundling output may have changed and this script needs updating." + ); + } + + // dist/.mjs is genuine ESM and really does have a default export, + // so the existing bundled declaration is already correct for it. + fs.writeFileSync(`dist/${variant}.d.mts`, esmContent); + + // dist/.js is UMD/CJS and does `module.exports = jsSHA` (not + // `module.exports.default = jsSHA`), so the CJS-facing declaration needs + // `export =` instead of `export default` or TypeScript will think + // consumers need a `.default` that doesn't exist at runtime. + const cjsContent = esmContent.replace("export { jsSHA as default };", "export = jsSHA;"); + fs.writeFileSync(`dist/${variant}.d.cts`, cjsContent); + + // The bare top-level "types" field (used by tooling that ignores the + // "exports" map entirely) is paired with "main", which is the CJS file - + // so it needs the same `export =` treatment. + fs.writeFileSync(srcPath, cjsContent); +}