Skip to content

otplib API Documentation / otplib / generateSecret

Function: generateSecret()

generateSecret(options?): string

Defined in: otplib/src/functional.ts:84

Generate a random secret key for use with OTP

The secret is encoded in Base32 format for compatibility with Google Authenticator and other authenticator apps.

Parameters

options?

Secret generation options

base32?

Base32Plugin

Base32 plugin to use (default: ScureBase32Plugin)

crypto?

CryptoPlugin

Crypto plugin to use (default: NobleCryptoPlugin)

length?

number

Number of random bytes to generate (default: 20) 20 bytes = 160 bits, which provides a good security margin

Returns

string

Base32-encoded secret key

Examples

ts
import { generateSecret } from 'otplib';

const secret = generateSecret();
// Returns: 'JBSWY3DPEHPK3PXP'
ts
import { generateSecret, NodeCryptoPlugin } from 'otplib';

const secret = generateSecret({
  crypto: new NodeCryptoPlugin(),
});

Released under the MIT License.