123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- 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<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[];
- }
- /**
- * 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<Int_64[], VariantType> {
- 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 };
|