Here's an example of a function to generate a one-time use passcode (OTP) in TypeScript using the Time-Based One-Time Password (TOTP) algorithm and the crypto
module for encryption:
index.ts656 chars21 lines
Here, secret
is a string that you should keep securely and distribute to the users or devices that you want to generate OTPs for. window
is an optional parameter specifying the time window of each OTP, in seconds. By default, it's set to 30 seconds as recommended by the TOTP algorithm.
To generate an OTP, simply call the generateOTP
function with the secret
and optionally the window
. It will return a six-digit alphanumeric string that serves as the OTP.
gistlibby LogSnag