Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

emit *.d.mts type files for esm entries #101

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions dist/sha.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
declare type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE";
declare type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY";
declare type GenericInputType = {
value: string;
format: "TEXT";
encoding?: EncodingType;
} | {
value: string;
format: "B64" | "HEX" | "BYTES";
} | {
value: ArrayBuffer;
format: "ARRAYBUFFER";
} | {
value: Uint8Array;
format: "UINT8ARRAY";
};
declare type FixedLengthOptionsNoEncodingType = {
hmacKey?: GenericInputType;
} | {
numRounds?: number;
};
declare 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;
}

declare 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: <INPUT>, format: <FORMAT>, encoding?: "UTF8" | "UTF16BE" | "UTF16LE"} where <FORMAT> takes the same
* values as `inputFormat` and <INPUT> can be a `string | ArrayBuffer | Uint8Array` depending on <FORMAT>.
* 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 };
163 changes: 163 additions & 0 deletions dist/sha1.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
declare type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE";
declare type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY";
declare type FormatType = "TEXT" | FormatNoTextType;
declare type GenericInputType = {
value: string;
format: "TEXT";
encoding?: EncodingType;
} | {
value: string;
format: "B64" | "HEX" | "BYTES";
} | {
value: ArrayBuffer;
format: "ARRAYBUFFER";
} | {
value: Uint8Array;
format: "UINT8ARRAY";
};
declare type FixedLengthOptionsNoEncodingType = {
hmacKey?: GenericInputType;
} | {
numRounds?: number;
};
declare type FixedLengthOptionsEncodingType = {
hmacKey?: GenericInputType;
encoding?: EncodingType;
} | {
numRounds?: number;
encoding?: EncodingType;
};
interface packedValue {
value: number[];
binLen: number;
}

declare abstract class jsSHABase<StateT, VariantT> {
/**
* @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<number[], "SHA-1"> {
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 };
Loading