Middle+
What is TOTP?
sobes.tech AI
Answer from AI
TOTP (Time-based One-Time Password) is an algorithm for generating one-time passwords based on the current time. It is used for two-factor authentication (2FA), where the user enters a temporary code that changes every 30-60 seconds.
How it works:
- A shared secret key exists on both the server and the client.
- A hash is computed from this key and the current time (usually rounded to 30-second intervals).
- A fixed-length numerical code (usually 6 digits) is extracted from the hash.
The advantage of TOTP is that the code changes over time and does not require constant connection to the server, only time synchronization and a shared secret.
Example usage includes Google Authenticator, Microsoft Authenticator, and other 2FA apps.
Example of generating TOTP in JavaScript with the otplib library:
const { totp } = require('otplib');
const secret = 'KVKFKRCPNZQUYMLXOVYDSQKJKZDTSRLD';
const token = totp.generate(secret);
console.log(token); // for example, '492039'