← 中文资源

简体中文版本

带 RFC 6238 测试向量的 TOTP 生成器

用 crypto 模块的 HMAC-SHA1 实现 8 位 TOTP,测试套件钉住 RFC 6238 附录全部六个官方向量。

阅读英文原文

目标

按 RFC 6238 生成基于时间的动态口令:计数器取 time/30 写入 8 字节大端缓冲,动态截断后模 10^8 并零填充。

要点

六个官方向量(t=59 得 94287082 等)全部由测试断言;–time 参数可复现任意历史时刻的口令。

提示

demo 密钥是 RFC 附录值;生产必须把密钥放保险库且绝不日志输出验证码。

验证

node –test 全部向量通过;node totp.mjs –time 59 输出 94287082 与 RFC 一致。 复现命令:

node --test totp.test.mjs
node totp.mjs --time 59

预期输出要点:Passing –time 59 with the RFC demo secret prints 94287082, matching the RFC 6238 appendix vector exactly.

实测证据:Node 22.23.2: node –test -> 2 passed; all six RFC 6238 appendix vectors matched; node totp.mjs –time 59 printed 94287082.