Skip to content

otplib API Documentation / @otplib/totp / generateSync

Function: generateSync() ​

generateSync(options): string

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

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

This is the synchronous version of generate. It requires a crypto plugin that supports synchronous HMAC operations (e.g., NodeCryptoPlugin or NobleCryptoPlugin). Using this with WebCryptoPlugin will throw an error.

Parameters ​

options ​

TOTPGenerateOptions

TOTP generation options

Returns ​

string

The TOTP code as a string

See ​

Throws ​

If the algorithm is invalid or unsupported by the plugin

Throws ​

If HMAC computation fails or the plugin does not support sync operations

Example ​

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

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

Released under the MIT License.