Skip to content

otplib API Documentation / @otplib/core / dynamicTruncate

Function: dynamicTruncate()

dynamicTruncate(hmacResult): number

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

Perform Dynamic Truncation as per RFC 4226 Section 5.3

The algorithm:

  1. Take the low-order 4 bits of the last byte as offset
  2. Extract 4 bytes starting at offset
  3. Mask the most significant bit to get a 31-bit unsigned integer

This ensures consistent extraction across different HMAC output sizes while producing a value that fits in a signed 32-bit integer.

Parameters

hmacResult

Uint8Array

HMAC result (at least 20 bytes for SHA-1)

Returns

number

Truncated 31-bit unsigned integer

See

RFC 4226 Section 5.3 - Generating an HOTP Value

Released under the MIT License.