2fa

中文文档
0.1.2发布于 2015/04/21MITCommonJS近 30 天下载 2.5万

TOTP + HOTP library, with nice utilities for handing 2FA

原始使用文档

2FA

Module for generating and verifying 2FA codes (specifically TOTP and HOTP).

Also contains utilities for handing 2FA logic, such as generating Google Authenticator compatible QR codes (without going via Google Charts) and generating backup codes.

Install

npm install --save 2fa

Usage

var tfa = require('2fa');

// lets generate a new key for a user
// tfa.generateKey(length (optional), cb)
tfa.generateKey(32, function(err, key) {
  // crypto secure hex key with 32 characters

  // generate crypto-secure backups codes in a user-friendly pattern
  // tfa.generateBackupCodes(num, pattern (optional), cb)
  tfa.generateBackupCodes(8, 'xxxx-xxxx-xxxx', function(err, codes) {
    // [ '7818-b7b8-c928', '3526-dc04-d3f2', 'be3c-5d9f-cb68', ... ]

    // these should be sent to the user, stored and checked when we get a 2fa code
  });

  // generate a google QR code so the user can save their new key
  // tfa.generateGoogleQR(name, accountname, secretkey, cb)
  tfa.generateGoogleQR('Company', '[email protected]', key, function(err, qr) {
    // data URL png image for google authenticator
  });

  var opts = {
    // the number of counters to check before what we're given
    // default: 0
    beforeDrift: 2,
    // and the number to check after
    // default: 0
    afterDrift: 2,
    // if before and after drift aren't specified,
    // before + after drift are set to drift / 2
    // default: 0
    drift: 4,
    // the step for the TOTP counter in seconds
    // default: 30
    step: 30
  };

  // calculate the counter for the HOTP (pretending it's actually TOTP)
  var counter = Math.floor(Date.now() / 1000 / opts.step);

  // generate a valid code (in real-life this will be user-input)
  var code = tfa.generateCode(key, counter);

  // verify it as a HOTP
  var validHOTP = tfa.verifyHOTP(key, code, counter, opts);
  // true

  // for TOTP, the counter is calculated internally using Date.now();
  var validTOTP = tfa.verifyTOTP(key, code, opts);
  // true

});

版本与发布记录

2fa 共发布 4 个版本,首次发布于 2014/09/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.2latest2015/04/2111 年前
0.1.12015/03/1311 年前
0.1.02015/03/0611 年前
0.0.12014/09/2212 年前

关于 2fa 的常见问题

2fa 是什么?

TOTP + HOTP library, with nice utilities for handing 2FA

如何安装 2fa?

在项目目录运行 npm install 2fa,或使用 pnpm add / yarn add / bun add。

2fa 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/2fa 是否存在。

2fa 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

2fa 可以商用吗?

2fa 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

2fa 的下载量有多少?

根据 npm 官方下载统计,2fa 在 2026/10/04 – 2026/11/02 这 30 天内被下载 25,214 次,日均约 840 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

2fa 还在维护吗?

最新版本 0.1.2 发布于 2015/04/21(11 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2014/09/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。