# 2fa 中文文档

规范地址：https://npmcn.com/package/2fa/

版本：0\.1\.2
npm 元数据更新时间：2022-06-12T14:06:55.958Z
中文译文更新时间：2026-11-05T20:40:31.889Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/2fa
原文对照：https://npmcn.com/package/2fa/?lang=original
项目主页：https://github.com/simontabor/2fa
源码仓库：https://github.com/simontabor/2fa

## 关键信息（来自 npm 元数据）

- 模块格式：CommonJS
- TypeScript 类型：未声明
- 运行依赖：2 个
- 首次发布：2014-09-21T20:55:34.615Z
- 最新版本发布：2015-04-21T11:04:03.575Z
- 近 30 天下载量：25214（2026-10-04 至 2026-11-02，npm 官方下载统计）
- 已发布版本数：4（近 12 个月 0 个）

## 中文简介

TOTP + HOTP 库, 并有很好的二FA 传送工具

## 安装

```sh
npm install 2fa
```

也可以使用：`pnpm add 2fa`、`yarn add 2fa`、`bun add 2fa`

## README

# 2个FA

生成和验证2FA代码的模块(特别是TOTP和HOTP).

还包含用于传递2FA逻辑的公用设备,例如生成Google认证器兼容的QR代码(不通过Google Charts)并生成备份代码.

## 安装

```
npm install --save 2fa
```

## 使用量

```js
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@gmail.com', 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 是什么？

TOTP \+ HOTP 库, 并有很好的二FA 传送工具

### 如何安装 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 和提交记录判断。

