UNPKG

gen-totp

Version:

A time-based One-time Password generator that uses current time as a source of uniqueness, following RFC 6238.

102 lines (101 loc) 3.88 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.genTOTP = exports.decToHex = exports.hexToDec = exports.base32ToHex = exports.leftPad = void 0; var jssha_1 = __importDefault(require("jssha")); /** * Left pads a string to a specified length. * @param {string} str - The string to pad. * @param {number} len - The desired length. * @param {string} pad - The padding character. * @returns {string} The padded string. */ function leftPad(str, len, pad) { return str.length >= len ? str : pad.repeat(len - str.length) + str; } exports.leftPad = leftPad; /** * Decodes a base32-encoded string to hexadecimal. * Supports RFC 4648 Base32. * @param {string} input - The base32 string. * @returns {string} The decoded hexadecimal string. */ function base32ToHex(input) { var base32Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; var cleanInput = input.toUpperCase().replace(/=+$/, ""); var bits = ""; var hex = ""; for (var _i = 0, cleanInput_1 = cleanInput; _i < cleanInput_1.length; _i++) { var char = cleanInput_1[_i]; var val = base32Chars.indexOf(char); if (val === -1) { throw new Error("Invalid base32 character: ".concat(char)); } bits += leftPad(val.toString(2), 5, "0"); } for (var i = 0; i + 4 <= bits.length; i += 4) { var chunk = bits.slice(i, i + 4); hex += parseInt(chunk, 2).toString(16); } return hex; } exports.base32ToHex = base32ToHex; /** * Converts a hexadecimal string to a decimal number. * @param {string} hex - The hexadecimal string. * @returns {number} The decimal number. */ function hexToDec(hex) { return parseInt(hex, 16); } exports.hexToDec = hexToDec; /** * Converts a decimal number to a hexadecimal string. * @param {number} dec - The decimal number. * @returns {string} The hexadecimal string. */ function decToHex(dec) { return leftPad(Math.round(dec).toString(16), 2, "0"); } exports.decToHex = decToHex; /** * Generates a TOTP (Time-based One-Time Password). * @param {string} key - The secret key. * @param {GenTOTPOptions} [options={}] - Configuration options. * @param {number} [options.period=30] - Time period in seconds. * @param {string} [options.algorithm='SHA-1'] - Hash algorithm. * @param {number} [options.digits=6] - Length of the resulting OTP. * @param {KeyEncoding} [options.encoding='utf8'] - Encoding of the key ('utf8', 'hex', or 'base32'). * @returns {string} The generated OTP. */ function genTOTP(key, options) { if (options === void 0) { options = {}; } var _a = options.period, period = _a === void 0 ? 30 : _a, _b = options.algorithm, algorithm = _b === void 0 ? "SHA-1" : _b, _c = options.digits, digits = _c === void 0 ? 6 : _c, _d = options.encoding, encoding = _d === void 0 ? "utf8" : _d; var epoch = Math.floor(Date.now() / 1000); var timeHex = leftPad(decToHex(Math.floor(epoch / period)), 16, "0"); var hexKey; if (encoding === "hex") { hexKey = key.toLowerCase(); } else if (encoding === "base32") { hexKey = base32ToHex(key); } else { // utf8 to hex var encoder = new TextEncoder(); hexKey = Array.from(encoder.encode(key)) .map(function (b) { return b.toString(16).padStart(2, "0"); }) .join(""); } var shaObj = new jssha_1.default(algorithm, "HEX"); shaObj.setHMACKey(hexKey, "HEX"); shaObj.update(timeHex); var hmac = shaObj.getHMAC("HEX"); var offset = hexToDec(hmac[hmac.length - 1]); var code = (hexToDec(hmac.slice(offset * 2, offset * 2 + 8)) & 0x7fffffff).toString(); return code.slice(-digits); } exports.genTOTP = genTOTP; exports.default = genTOTP;