otp-io
Version:
🕖 Typed library to work 2fa via Google Authenticator/Time-based TOTP/Hmac-based HOTP
16 lines • 707 B
JavaScript
import {getBigIntBytes}from'./bytes.mjs';import {getDefaultHOTPOptions}from'./hotp.options.mjs';/**
* 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({}, getDefaultHOTPOptions()), options);
const digest = await hmac(merged.algorithm, merged.secret.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);
}export{hotp};