Skip to content

otplib API Documentation / @otplib/core / truncateDigits

Function: truncateDigits()

truncateDigits(value, digits): string

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

Convert truncated integer to OTP string with specified digits

Computes: Snum mod 10^Digit (RFC 4226 Section 5.3)

The result is zero-padded to ensure consistent length, as required for proper token comparison.

Parameters

value

number

The truncated integer value (Snum)

digits

number

Number of digits for the OTP (Digit, typically 6-8)

Returns

string

OTP string with leading zeros if necessary

See

RFC 4226 Section 5.3 - Generating an HOTP Value

Released under the MIT License.