注意:这篇文章上次更新于822天前,文章内容可能已经过时。
This article was last updated822 days ago, the content may be outdated.
--
背景
这是一个完全由 AI 编写的小工具,用于生成 TOTP 密钥。
让我来详细解释一下它是用来干什么的。
简单来说,就是为了解决下面这个问题的:

两三年前,手机下载了微软的 Authenticator,然后我的微软账号每次登录就一直要求我用 Authenticator 来认证。我当时就在想,如果我手机丢了怎么办,还有什么办法找回我的账号吗?
网上搜索了一圈,得到的结论基本就是“极其困难”。
于是,我便开始小心翼翼的维护这个软件,中间换了一次手机,我也是把 Authenticator 的数据迁移过去,以免丢失。
但是,这还是没有解决那个问题:如果我手机丢了呢?
后来,我发现了这个项目:https://github.com/Bubka/2FAuth
A web app to manage your Two-Factor Authentication (2FA) accounts and generate their security codes
这个项目可以用来生成二步验证的验证码,而且重点是 “web app”,也就是说,它不再依赖我的手机,只要我把它在电脑或者服务器上运行起来,我就可以随时随地的获取到我的验证码而不用担心手机丢失。
(当然,你需要对这样重要的数据进行备份,毕竟即使是电脑和服务器也是会坏的。)
在查询这个项目怎么应用的过程中,我发现了另一个专业的密码管理器也能用于生成二步验证的验证码,那就是 Bitwarden。
它有一个非官方的开源实现:https://github.com/dani-garcia/vaultwarden
那么更进一步的问题是,他们是怎么生成这些验证码的呢?
答案就是 TOTP。
TOTP 是 Time-based One-Time Password 的缩写,即基于时间的一次性密码。它是一种用于身份验证的算法,通常用于多因素身份验证(MFA)。
也就是说,它根据一个输入的密钥(也就是一个字符串)和当前的时间戳生成一个一次性的密码,微软,甲骨文,谷歌… 等等几乎所有国外的公司都在使用这种算法。
无论是甲骨文云的验证器、GitHub 的验证器、微软的验证器、谷歌的验证器,都是基于这个算法的一个 APP 而已。
当这些国外网站要求我们开启两步验证的时候,你需要点击一些非推荐性的选项,例如:我不能下载 APP,我不能扫描二维码,我选择手动输入密钥 等等
然后你就会获得一个 TOTP 密钥,像是这样的字符串:JBSWY3DPEHPK3PXP
把它输入到这里,你就获得了一个验证码。

你需要保存好你的密钥,因为这个密钥是生成验证码的唯一凭证。
注意它是 Time-based 的算法,使用它请确保你的设备时间准确。
30s 的刷新周期和 6 位的验证码是默认的设置,绝大多数情况不需要修改。
代码
1 | <style> |
Background
This is a small tool entirely written by AI, used to generate TOTP keys.
Let me explain in detail what it does.
In short, it solves the following problem:

Two or three years ago, I installed Microsoft Authenticator on my phone, and ever since, every login to my Microsoft account has demanded authentication via Authenticator. I kept thinking — what if I lose my phone? Is there any other way to recover my account?
After searching the internet, the general conclusion was “extremely difficult”.
So I started maintaining that app very carefully. When I switched phones in between, I migrated the Authenticator data over so nothing would be lost.
But that still didn’t solve the core problem: what if I lose my phone?
Later, I found this project: https://github.com/Bubka/2FAuth
A web app to manage your Two-Factor Authentication (2FA) accounts and generate their security codes
This project can generate two-factor verification codes, and the key point is it’s a “web app” — it no longer depends on my phone. As long as I run it on a computer or server, I can get my codes anytime, anywhere without worrying about losing my phone.
(Of course, you still need to back up such important data — computers and servers can break too.)
While researching how to use this project, I discovered another professional password manager that can also generate two-factor codes: Bitwarden.
It has an unofficial open-source implementation: https://github.com/dani-garcia/vaultwarden
So the further question is: how do they generate these codes?
The answer is TOTP.
TOTP stands for Time-based One-Time Password. It’s an algorithm used for authentication, typically for multi-factor authentication (MFA).
That is, it generates a one-time password from an input secret (i.e. a string) and the current timestamp. Microsoft, Oracle, Google… virtually every foreign company uses this algorithm.
Whether it’s Oracle Cloud’s authenticator, GitHub’s authenticator, Microsoft’s authenticator, or Google’s authenticator, they’re all just apps built on this algorithm.
When these foreign sites ask you to enable two-factor verification, you need to click some non-recommended options like: I can’t download apps, I can’t scan QR codes, I choose to enter the secret manually, etc.
Then you’ll get a TOTP secret, a string like: JBSWY3DPEHPK3PXP
Enter it here and you’ll get a code.

Keep your secret safe — it’s the only credential for generating codes.
Note that it’s a time-based algorithm; make sure your device’s clock is accurate.
The 30s refresh period and 6-digit codes are the defaults; in most cases you won’t need to change them.
Code
1 | <style> |


