Skip to content

otplib API Documentation / @otplib/core / utils / normalizeCounterTolerance

Function: normalizeCounterTolerance()

normalizeCounterTolerance(counterTolerance?): [number, number]

Defined in: packages/core/src/utils.ts:781

Normalize counter tolerance to [past, future] tuple

Converts a number or tuple counter tolerance specification into a [past, future] tuple

  • Number: creates look-ahead only tolerance [0, tolerance] (default for security)
  • Tuple: uses the tuple as-is (past, future)

The default behavior (number → look-ahead only) improves security by preventing replay attacks. HOTP counters should only move forward in normal operation.

Parameters

counterTolerance?

Counter tolerance specification (number or tuple [past, future])

number | [number, number]

Returns

[number, number]

Tuple [past, future] representing counters to check

Example

ts
normalizeCounterTolerance(0)        // [0, 0]
normalizeCounterTolerance(5)        // [0, 5] - look-ahead only (secure default)
normalizeCounterTolerance([10, 5])  // [10, 5] - explicit past/future
normalizeCounterTolerance([5, 5])   // [5, 5] - explicit symmetric (use with caution)

Released under the MIT License.