totp-generator
Version:
Generate TOTP tokens from key
85 lines (84 loc) • 3.49 kB
TypeScript
export type TOTPAlgorithm = "SHA-1" | "SHA-256" | "SHA-384" | "SHA-512";
export type TOTPEncoding = "hex" | "ascii";
/**
* Options for TOTP generation.
* @param {number} [digits=6] - The number of digits in the OTP.
* @param {TOTPAlgorithm} [algorithm="SHA-1"] - Algorithm used for hashing.
* @param {TOTPEncoding} [encoding="hex"] - Encoding used for the OTP.
* @param {number} [period=30] - The time period for OTP validity in seconds.
* @param {number} [timestamp=Date.now()] - The current timestamp.
* @param {boolean} [explicitZeroPad=false] - If true, pads the OTP with leading zeros to match the desired number of digits.
*/
type Options = {
digits?: number;
algorithm?: TOTPAlgorithm;
encoding?: TOTPEncoding;
period?: number;
timestamp?: number;
explicitZeroPad?: boolean;
};
export declare class TOTP {
/**
* An internal, swappable function for performing HMAC signing.
* This allows replacement of the crypto implementation.
* @internal
* @param {ArrayBuffer} keyBuffer - The secret key.
* @param {string} dataHex - The data to sign, as a hexadecimal string.
* @param {TOTPAlgorithm} algorithm - The hashing algorithm to use.
* @returns {Promise<ArrayBuffer>} A promise that resolves to the signature.
*/
private static _sign;
/**
* Generates a Time-based One-Time Password (TOTP).
* @async
* @param {string} key - The secret key for TOTP.
* @param {Options} options - Optional parameters for TOTP.
* @returns {Promise<{otp: string, expires: number}>} A promise that resolves to an object containing the OTP and its expiry time.
*/
static generate(key: string, options?: Options): Promise<{
otp: string;
expires: number;
}>;
/**
* Converts a hexadecimal string to a decimal number.
* @param {string} hex - The hex string.
* @returns {number} The decimal representation.
*/
private static hex2dec;
/**
* Converts a decimal number to a hexadecimal string.
* @param {number} dec - The decimal number.
* @returns {string} The hex representation.
*/
private static dec2hex;
/**
* Converts a base32 encoded string to an ArrayBuffer.
* @param {string} str - The base32 encoded string to convert.
* @returns {ArrayBuffer} The ArrayBuffer representation of the base32 encoded string.
*/
private static base32ToBuffer;
/**
* Converts an ASCII string to an ArrayBuffer.
* @param {string} str - The ASCII string to convert.
* @returns {ArrayBuffer} The ArrayBuffer representation of the ASCII string.
*/
private static asciiToBuffer;
/**
* Converts a hexadecimal string to an ArrayBuffer.
* @param {string} hex - The hexadecimal string to convert.
* @returns {ArrayBuffer} The ArrayBuffer representation of the hexadecimal string.
*/
private static hex2buf;
/**
* Converts an ArrayBuffer to a hexadecimal string.
* @param {ArrayBuffer} buffer - The ArrayBuffer to convert.
* @returns {string} The hexadecimal string representation of the buffer.
*/
private static buf2hex;
/**
* A precalculated mapping from base32 character codes to their corresponding index values for performance optimization.
* This mapping is used in the base32ToBuffer method to convert base32 encoded strings to their binary representation.
*/
private static readonly base32;
}
export {};