Skip to content

otplib API Documentation / @otplib/totp / generate

Function: generate()

generate(options): Promise<string>

Defined in: totp/src/index.ts:122

Generate a Time-based One-Time Password (TOTP)

Implements the TOTP algorithm as specified in RFC 6238 Section 4:

T = (Current Unix time - T0) / X
TOTP = HOTP(K, T)

Where:

  • T0 is the Unix time to start counting time steps (default 0, per RFC 6238 Section 4.1)
  • X is the time step in seconds (default 30, per RFC 6238 Section 4.1)
  • K is the shared secret key

Parameters

options

TOTPGenerateOptions

TOTP generation options

Returns

Promise<string>

The TOTP code as a string

See

Example

ts
import { generate } from '@otplib/totp';
import { NodeCryptoPlugin } from '@otplib/plugin-crypto-node';

const totp = generate({
  secret: new Uint8Array([1, 2, 3, 4, 5]),
  time: Date.now() / 1000,
  period: 30,
  digits: 6,
  crypto: new NodeCryptoPlugin(),
});
// Returns: '123456'

Released under the MIT License.