otplib API Documentation / @otplib/totp / generateSync
Function: generateSync()
generateSync(
options):string
Defined in: totp/src/index.ts:156
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
TOTP generation options
Returns
string
The TOTP code as a string
See
- generate for the async version
- RFC 6238 Section 4
Throws
If the crypto plugin doesn't 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'