# TOTP

**TOTP**（基于时间的一次性密码）是一种算法，由 RFC 6238 于 2011 年 5 月规范。它将基于 HMAC 的一次性密码（HOTP）算法应用于时间值而非事件计数器，从而生成短时效的一次性密码。<sup>[1](https://datatracker.ietf.org/doc/html/rfc6238)</sup> 共享密钥与当前时间（划分为固定步长）共同产生验证码，客户端设备与服务器均可独立计算，因此登录时无需网络连接。TOTP 被广泛部署为双因素与多因素认证中的第二因素，最直观的体现是 [Google Authenticator](https://www.edgepedia.org/zh/google-authenticator) 等身份验证器应用。<sup>[2](https://security.googleblog.com/2023/04/google-authenticator-now-supports.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 标准 | RFC 6238，2011 年 5 月由 IETF 发布；是 HOTP（RFC 4226，2005 年 12 月）的扩展，将移动因子改为基于时间<sup>[1](https://datatracker.ietf.org/doc/html/rfc6238)</sup> |
| 核心计算 | \( \mathrm{TOTP}(K, T) = \mathrm{Truncate}(\mathrm{HMAC}\text{-}\mathrm{SHA}\text{-}1(K, T)) \)，其中时间值 \( T \) 取代了 HOTP 的计数器 \( C \)<sup>[1](https://datatracker.ietf.org/doc/html/rfc6238)</sup> |
| 默认参数 | 时间步长 \( X = 30 \) 秒；起始时间 \( T_0 = 0 \)（Unix 纪元）；验证码至少 6 位<sup>[1](https://datatracker.ietf.org/doc/html/rfc6238)</sup><sup> • </sup><sup>[3](https://datatracker.ietf.org/doc/html/rfc4226.txt)</sup> |
| 哈希函数 | 与 HOTP 一样使用 HMAC-SHA-1；实现也可使用 HMAC-SHA-256 或 HMAC-SHA-512<sup>[1](https://datatracker.ietf.org/doc/html/rfc6238)</sup> |
| 共享密钥 | 至少 128 位，推荐 160 位（HOTP 的要求）<sup>[3](https://datatracker.ietf.org/doc/html/rfc4226.txt)</sup> |
| 抗钓鱼能力 | 无；OTP 验证器不具备抗验证方冒充能力，在伪造网站上输入的验证码可被转发至真实网站<sup>[4](https://www.nist.gov/document/sp-800-63-3-implementation-resources07012020pdf)</sup><sup> • </sup><sup>[5](https://www.idmanagement.gov/playbooks/altauthn/)</sup> |
| 时钟容差 | 服务器通常接受当前 30 秒窗口及其前后相邻窗口，以吸收时钟漂移<sup>[6](https://www.usenix.org/system/files/soups2019-reese.pdf)</sup> |

## 算法工作原理

TOTP 直接建立在 HOTP 之上。在 HOTP 中，客户端与服务器共享密钥 \( K \) 和一个 8 字节计数器 \( C \)，密码是对这两个值进行 HMAC-SHA-1 计算后截断所得的结果；计数器每次使用后递增。TOTP 保持相同的构造，但将计数器替换为从时间派生的值：当前 Unix 时间减去起始时间 \( T_0 \)，再除以时间步长 \( X \)，即得到移动因子。<sup>[1](https://datatracker.ietf.org/doc/html/rfc6238)</sup>

默认时间步长为 30 秒，默认起始时间为 Unix 纪元，即 \( T_0 = 0 \)。两者都是在配置阶段确立的系统参数，由证明方与验证方共享。在 30 秒步长下，窗口内任意时刻生成的验证码在窗口结束前一直有效，因此用户输入验证码的时间可能短至 30 秒。<sup>[1](https://datatracker.ietf.org/doc/html/rfc6238)</sup><sup> • </sup><sup>[6](https://www.usenix.org/system/files/soups2019-reese.pdf)</sup> RFC 要求实现必须在 Unix 时间超过 2038 年后仍支持大于 32 位整数的时间值。<sup>[1](https://datatracker.ietf.org/doc/html/rfc6238)</sup>

HOTP 的截断步骤将 160 位 HMAC 输出转换为简短十进制验证码：动态截断从 HMAC 结果中提取一个 31 位字符串，验证码即该数对 \( 10^{\mathrm{Digit}} \) 取模的结果。HOTP 规范要求至少 6 位数值，并建议仅输出数字，以便在手机等受限设备上轻松输入。<sup>[3](https://datatracker.ietf.org/doc/html/rfc4226.txt)</sup>

**验证与漂移。** 由于验证码依赖时钟，客户端与服务器必须保持合理同步。验证服务器通常不仅将提交的验证码与当前时间步比较，还与可接受传输延迟窗口内的过去时间步比较；RFC 建议最多允许一个时间步的网络延迟，因为较大的延迟窗口会扩大攻击面。<sup>[1](https://datatracker.ietf.org/doc/html/rfc6238)</sup> 常见实践中，服务器接受当前 30 秒窗口及其前后相邻窗口。<sup>[6](https://www.usenix.org/system/files/soups2019-reese.pdf)</sup>

## 安全属性与局限

**重放与猜测。** NIST 的数字身份指南要求验证方在 OTP 有效期内仅接受一次，以提供抗重放能力，并要求通过经过认证的保护通道收集 OTP，以抵御窃听和中间人攻击。若验证器输出短于 64 位，验证方必须实施速率限制；连续失败尝试的一般上限为 100 次，该数字的选择兼顾了六位十进制 OTP 输出下正确猜测的概率与账户恢复的需要。<sup>[7](https://pages.nist.gov/800-63-4/sp800-63b/authenticators/)</sup> 共享密钥及其算法在当前 SP 800-131A 修订版下必须提供至少 112 位安全强度。<sup>[7](https://pages.nist.gov/800-63-4/sp800-63b/authenticators/)</sup>

**钓鱼。** TOTP 不将验证码绑定到所访问的网站，因此用户可能被骗在攻击者控制的页面上输入有效验证码，该验证码可被转发并立即使用。NIST 的实施资源指出，OTP 设备从不被视为具备抗验证方冒充能力，美国联邦抗钓鱼验证器playbook将移动 TOTP 列为各机构使用的可被钓鱼和可重放的选项之一。<sup>[4](https://www.nist.gov/document/sp-800-63-3-implementation-resources07012020pdf)</sup><sup> • </sup><sup>[5](https://www.idmanagement.gov/playbooks/altauthn/)</sup> CISA 敦促各组织采用抗钓鱼 MFA，CMS 政策仅在抗钓鱼选项不可行且已记录风险接受的情况下才允许 OTP 验证器。<sup>[8](https://content.govdelivery.com/accounts/USDHSCISA/bulletins/3355d59)</sup><sup> • </sup><sup>[9](https://security.cms.gov/posts/ciso-memo-26-05-authentication-requirements-phishing-resistant-access)</sup> FIDO2/WebAuthn 等抗钓鱼替代方案通过公钥密码学将验证器绑定到验证方的域名。<sup>[10](https://pages.nist.gov/800-63-4/sp800-63b.html)</sup>

**密钥存储。** TOTP 系统需要在客户端和服务器两端存储共享密钥，因此服务器一旦被攻破，系统中每个用户的第二因素都可能暴露。<sup>[11](https://arxiv.org/abs/1708.08424)</sup> 在客户端，密钥是生成验证码的输入，因此单向哈希无法保护它；如果攻击者从手机或服务器窃取密钥，就能冒充用户。<sup>[6](https://www.usenix.org/system/files/soups2019-reese.pdf)</sup>

## 部署与可用性

设置通常包括扫描服务显示的[二维码](https://www.edgepedia.org/zh/qr-code)，该二维码将签发方名称、账户标签和随机密钥传输到身份验证器应用。<sup>[12](https://faculty.ksu.edu.sa/sites/default/files/usenixsecurity23-gilsenan.pdf)</sup> 此后，验证码在本地生成并可离线工作，这消除了影响短信验证码的蜂窝网络投递攻击面，并消除了对电话号码的依赖。<sup>[6](https://www.usenix.org/system/files/soups2019-reese.pdf)</sup> 一项针对五种双因素方法的可用性研究发现，TOTP 在投递方面比短信更快、更不易出错，但指出验证码仍需手动输入，且所引 Pew 数据显示智能手机拥有率为 77%（美国人），与短信相比限制了可部署性。<sup>[6](https://www.usenix.org/system/files/soups2019-reese.pdf)</sup>

**备份与恢复。** 丢失保存密钥的设备会导致用户无法登录，因此应用提供备份机制。2023 年 USENIX Security 的一项研究调查了所有安装量至少 10 万且具备备份机制的通用 Android TOTP 应用（共 22 款），发现大多数备份策略将信任寄托于 TOTP 本应取代的同一批技术：密码、短信和电子邮件。两款应用将明文 TOTP 密钥发送给应用开发者，四款将加密备份及其加密密钥一并发送给开发者，超过半数允许明文备份。<sup>[12](https://faculty.ksu.edu.sa/sites/default/files/usenixsecurity23-gilsenan.pdf)</sup> 恢复流程本身是已知薄弱点：一项针对 1,303 个提供 MFA 的网站的研究发现，许多网站部署了不安全的恢复流程，使能够访问关联电子邮件地址的攻击者可以完全禁用 MFA。<sup>[13](https://arxiv.org/abs/2306.09708)</sup>

**云同步。** Google 于 2010 年发布 Google Authenticator，并于 2023 年 4 月增加了将一次性验证码备份到 Google 账户的功能，以解决丢失设备导致的锁定问题。<sup>[2](https://security.googleblog.com/2023/04/google-authenticator-now-supports.html)</sup> 该功能招致批评，因为同步的密钥未进行端到端加密，用户只能通过选择退出来将验证码仅保存在本地。<sup>[14](https://www.techtarget.com/cybersecurity/news/366555652/Google-Authenticator-synchronization-raises-MFA-concerns)</sup> 2023 年 9 月，工程公司 Retool 将一起社会工程攻击事件的部分原因归咎于该同步功能：一名员工被语音钓鱼攻击欺骗，分享了凭据和 MFA 验证码，攻击者注册了自己的设备，随后同步的验证码使其得以访问该员工跨系统的 MFA 令牌。该事件暴露了 27 个加密货币行业客户账户。<sup>[15](https://arstechnica.com/security/2023/09/how-google-authenticator-gave-attackers-one-companys-keys-to-the-kingdom/)</sup>

## 在认证方法中的位置

在 NIST 的验证器分类中，TOTP 表现为单因素 OTP 验证器，或者当设备在显示验证码前需要 PIN 或生物识别等激活因素时，表现为多因素 OTP。<sup>[7](https://pages.nist.gov/800-63-4/sp800-63b/authenticators/)</sup> 与短信投递相比，TOTP 避免了网络拦截和 SIM 卡交换攻击；与推送通知相比，它避免了 MFA 疲劳攻击，但需要手动输入验证码；与 FIDO2 通行密钥和安全密钥相比，它缺乏抗钓鱼能力和验证方名称绑定。<sup>[6](https://www.usenix.org/system/files/soups2019-reese.pdf)</sup><sup> • </sup><sup>[10](https://pages.nist.gov/800-63-4/sp800-63b.html)</sup> NIST 于 2025 年 7 月最终确定了数字身份指南第 4 修订版，继续允许 OTP 验证器，同时在最高保证级别要求抗钓鱼能力。<sup>[16](https://csrc.nist.gov/pubs/sp/800/63/b/4/final)</sup>

## References

1. [M'Raihi, D. et al. (2011). TOTP: Time-Based One-Time Password Algorithm. RFC 6238, IETF.](https://datatracker.ietf.org/doc/html/rfc6238)
2. [Brand, C. (2023). Google Authenticator now supports Google Account synchronization. Google Online Security Blog.](https://security.googleblog.com/2023/04/google-authenticator-now-supports.html)
3. [M'Raihi, D. et al. (2005). HOTP: An HMAC-Based One-Time Password Algorithm. RFC 4226, IETF.](https://datatracker.ietf.org/doc/html/rfc4226.txt)
4. [NIST SP 800-63-3 Implementation Resources (2020).](https://www.nist.gov/document/sp-800-63-3-implementation-resources07012020pdf)
5. [Phishing-Resistant Authenticator Playbook. IDManagement.gov (U.S. federal CIO council).](https://www.idmanagement.gov/playbooks/altauthn/)
6. [Reese, K. et al. (2019). A Usability Study of Five Two-Factor Authentication Methods. SOUPS, USENIX.](https://www.usenix.org/system/files/soups2019-reese.pdf)
7. [NIST SP 800-63B-4, Digital Identity Guidelines: Authentication and Authenticator Management, Authenticators section.](https://pages.nist.gov/800-63-4/sp800-63b/authenticators/)
8. [CISA (2022). CISA Releases Guidance on Phishing-Resistant and Numbers Matching Multifactor Authentication.](https://content.govdelivery.com/accounts/USDHSCISA/bulletins/3355d59)
9. [CISO Memo 26-05: Authentication Requirements for Phishing-Resistant Access. CMS.](https://security.cms.gov/posts/ciso-memo-26-05-authentication-requirements-phishing-resistant-access)
10. [NIST SP 800-63B (Rev. 4), Digital Identity Guidelines.](https://pages.nist.gov/800-63-4/sp800-63b.html)
11. [Camenisch, J. et al. (2017). T/Key: Second-Factor Authentication From Secure Hash Chains. arXiv.](https://arxiv.org/abs/1708.08424)
12. [Gilsenan, M. et al. (2023). Security and Privacy Failures in Popular 2FA Apps. USENIX Security Symposium.](https://faculty.ksu.edu.sa/sites/default/files/usenixsecurity23-gilsenan.pdf)
13. ["We've Disabled MFA for You": An Evaluation of the Security and Usability of Multi-Factor Authentication Recovery Deployments. arXiv.](https://arxiv.org/abs/2306.09708)
14. [Google Authenticator synchronization raises MFA concerns. TechTarget (2023).](https://www.techtarget.com/cybersecurity/news/366555652/Google-Authenticator-synchronization-raises-MFA-concerns)
15. [Goodin, D. (2023). How Google Authenticator made one company's network breach much, much worse. Ars Technica.](https://arstechnica.com/security/2023/09/how-google-authenticator-gave-attackers-one-companys-keys-to-the-kingdom/)
16. [SP 800-63B-4, Digital Identity Guidelines: Authentication and Authenticator Management (Final, July 2025). NIST CSRC.](https://csrc.nist.gov/pubs/sp/800/63/b/4/final)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › 密码算法与协议

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/totp

English original: https://www.edgepedia.org/totp
