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:
- Take the low-order 4 bits of the last byte as offset
- Extract 4 bytes starting at offset
- 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