UNPKG

gen-totp

Version:

A time-based One-time Password generator that uses current time as a source of uniqueness, following RFC 6238.

127 lines (126 loc) 4.67 kB
/** * 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, timestamp?: number): string; /** * Options for verifying a TOTP. */ export interface VerifyTOTPOptions extends GenTOTPOptions { window?: number; } /** * Verifies a TOTP (Time-based One-Time Password). * @param {string} key - The secret key. * @param {string} token - The token to verify. * @param {VerifyTOTPOptions} [options={}] - Configuration options. * @returns {boolean} True if the token is valid, false otherwise. */ export declare function verifyTOTP(key: string, token: string, options?: VerifyTOTPOptions, timestamp?: number): boolean; /** * Options for generating an otpauth URI. */ export interface OtpAuthUriOptions { accountName: string; issuer: string; period?: number; algorithm?: FixedLengthVariantType; digits?: number; } /** * Generates an otpauth URI for QR code generation. * @param {string} key - The base32-encoded secret key. * @param {OtpAuthUriOptions} options - Configuration options. * @returns {string} The otpauth URI. */ export declare function generateOtpAuthUri(key: string, options: OtpAuthUriOptions): string; /** * Encodes a byte array to a base32 string. * @param {Uint8Array} bytes - The bytes to encode. * @returns {string} The base32-encoded string. */ export declare function bytesToBase32(bytes: Uint8Array): string; /** * Generates a cryptographically secure secret key. * @param {number} [length=20] - The length of the key in bytes. * @returns {string} The base32-encoded secret key. */ export declare function generateSecretKey(length?: number): string; /** * Options for generating an HOTP. */ export interface GenHOTPOptions { algorithm?: FixedLengthVariantType; digits?: number; encoding?: KeyEncoding; } /** * Generates an HOTP (HMAC-based One-Time Password). * @param {string} key - The secret key. * @param {number} counter - The counter value. * @param {GenHOTPOptions} [options={}] - Configuration options. * @returns {string} The generated OTP. */ export declare function genHOTP(key: string, counter: number, options?: GenHOTPOptions): string; /** * Options for verifying an HOTP. */ export interface VerifyHOTPOptions extends GenHOTPOptions { window?: number; } /** * Verifies an HOTP (HMAC-based One-Time Password). * @param {string} key - The secret key. * @param {string} token - The token to verify. * @param {number} counter - The current counter value. * @param {VerifyHOTPOptions} [options={}] - Configuration options. * @returns {{newCounter: number} | null} The new counter value if the token is valid, otherwise null. */ export declare function verifyHOTP(key: string, token: string, counter: number, options?: VerifyHOTPOptions): { newCounter: number; } | null; export default genTOTP;