UNPKG

otp-io

Version:

🕖 Typed library to work 2fa via Google Authenticator/Time-based TOTP/Hmac-based HOTP

16 lines • 724 B
'use strict';const bytes=require('./bytes.js'),hotp_options=require('./hotp.options.js');/** * Generates HOTP code from secret key, counter and options * * @export * @param {Hmac} hmac * @param {HOTPOptions} options * @return {Promise<string>} */ async function hotp(hmac, options) { const merged = Object.assign(Object.assign({}, hotp_options.getDefaultHOTPOptions()), options); const digest = await hmac(merged.algorithm, merged.secret.bytes, bytes.getBigIntBytes(merged.counter)); const offset = digest[digest.length - 1] & 0xf; const view = new DataView(digest.buffer); const binary = view.getUint32(offset) & 2147483647; return binary.toString().slice(-merged.digits); }exports.hotp=hotp;