Skip to content

otplib API Documentation / @otplib/hotp / generate

Function: generate()

generate(options): Promise<string>

Defined in: hotp/src/index.ts:96

Generate an HMAC-based One-Time Password (HOTP)

Implements the HOTP algorithm as specified in RFC 4226 Section 5.3:

  1. Convert counter to 8-byte big-endian array (RFC 4226 Section 5.1)
  2. Compute HMAC-SHA-1 using the secret key and counter (RFC 4226 Section 5.2)
  3. Apply dynamic truncation to extract 4-byte code (RFC 4226 Section 5.3)
  4. Reduce modulo 10^digits to get final OTP (RFC 4226 Section 5.3)

Parameters

options

HOTPGenerateOptions

HOTP generation options

Returns

Promise<string>

The HOTP code as a string

See

RFC 4226 Section 5.3 - Generating an HOTP Value

Example

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

const hotp = generate({
  secret: new Uint8Array([1, 2, 3, 4, 5]),
  counter: 0,
  digits: 6,
  crypto: new NodeCryptoPlugin(),
});
// Returns: '123456'

Released under the MIT License.