native-browser-otp
Version:
Lightweight library for generating TOTP and HOTP codes using browser's native Web Cryptography API. Perfect for implementing two-factor authentication (2FA) in web applications.
45 lines (44 loc) • 1.56 kB
TypeScript
/**
* The HOTP algorithm is based on an increasing counter value (C) and a
* static symmetric key known only to the token and the validation
* service (K). In order to create the HOTP value.
*
* We can describe the operations in 3 distinct steps:
*
* Step 1: Generate an HMAC-SHA-1 value Let HS = HMAC-SHA-1(K,C) // HS
* is a 20-byte string
*
* Step 2: Generate a 4-byte string (Dynamic Truncation) (see truncate() above)
*
* Step 3: Compute an HOTP value
* Let Snum = StToNum(Sbits) // Convert S to a number in
* 0...2^{31}-1
* Return D = Snum mod 10^Digit // D is a number in the range
* 0...10^{Digit}-1
*
* See: https://datatracker.ietf.org/doc/html/rfc4226
*
* @param secret - static symmetric key known only to the token and the validation
* service
* @param counter - an increasing counter value
* @returns OTP
*/
export declare function hotp(secret: string, counter: number): Promise<string>;
/**
* TOTP is the time-based variant of this algorithm, where a value T,
* derived from a time reference and a time step, replaces the counter C
* in the HOTP computation.
*
* See: https://datatracker.ietf.org/doc/html/rfc6238
*
* @param secret - static symmetric key known only to the token and the validation
* service
* @returns OTP
*/
export declare function totp(secret: string): Promise<string>;
/**
* Remaining seconds until the next TOTP code is generated.
*
* @returns Remaining seconds
*/
export declare function timeLeft(): number;