gen-totp
Version:
A time-based One-time Password generator that uses current time as a source of uniqueness, following RFC 6238.
51 lines (50 loc) • 1.96 kB
TypeScript
/**
* Left pads a string to a specified length.
* @param {string} str - The string to pad.
* @param {number} len - The desired length.
* @param {string} pad - The padding character.
* @returns {string} The padded string.
*/
export declare function leftPad(str: string, len: number, pad: string): string;
/**
* Decodes a base32-encoded string to hexadecimal.
* Supports RFC 4648 Base32.
* @param {string} input - The base32 string.
* @returns {string} The decoded hexadecimal string.
*/
export declare function base32ToHex(input: string): string;
/**
* Converts a hexadecimal string to a decimal number.
* @param {string} hex - The hexadecimal string.
* @returns {number} The decimal number.
*/
export declare function hexToDec(hex: string): number;
/**
* Converts a decimal number to a hexadecimal string.
* @param {number} dec - The decimal number.
* @returns {string} The hexadecimal string.
*/
export declare function decToHex(dec: number): string;
export type FixedLengthVariantType = "SHA-1" | "SHA-224" | "SHA-256" | "SHA-384" | "SHA-512" | "SHA3-224" | "SHA3-256" | "SHA3-384" | "SHA3-512";
export type KeyEncoding = "utf8" | "hex" | "base32";
/**
* Options for generating a TOTP.
*/
interface GenTOTPOptions {
period?: number;
algorithm?: FixedLengthVariantType;
digits?: number;
encoding?: KeyEncoding;
}
/**
* Generates a TOTP (Time-based One-Time Password).
* @param {string} key - The secret key.
* @param {GenTOTPOptions} [options={}] - Configuration options.
* @param {number} [options.period=30] - Time period in seconds.
* @param {string} [options.algorithm='SHA-1'] - Hash algorithm.
* @param {number} [options.digits=6] - Length of the resulting OTP.
* @param {KeyEncoding} [options.encoding='utf8'] - Encoding of the key ('utf8', 'hex', or 'base32').
* @returns {string} The generated OTP.
*/
export declare function genTOTP(key: string, options?: GenTOTPOptions): string;
export default genTOTP;