javascript - 为什么在这个 TOTP 函数中有时会得到 5 位而不是 6 位?
问题描述
我在我的 Node 应用程序中使用来自https://hackernoon.com/how-to-implement-google-authenticator-two-factor-auth-in-javascript-091wy3vh3的代码,它生成基于时间的 OTP 6 位代码。
它工作正常,但是,它时不时地只返回一个 5 位数的代码而不是 6 位数。
function generateHOTP(secret, counter) {
const decodedSecret = base32.decode.asBytes(secret);
const buffer = Buffer.alloc(8);
for (let i = 0; i < 8; i++) {
buffer[7 - i] = counter & 0xff;
counter = counter >> 8;
}
// Step 1: Generate an HMAC-SHA-1 value
const hmac = crypto.createHmac('sha1', Buffer.from(decodedSecret));
hmac.update(buffer);
const hmacResult = hmac.digest();
// Step 2: Generate a 4-byte string (Dynamic Truncation)
const code = dynamicTruncationFn(hmacResult);
// Step 3: Compute an HOTP value
return code % 10**6;
//return code;
}
解决方案
推荐阅读
- javascript - 在Javascript中,保留关键字“short”有什么作用?
- database - Laravel 消息:违反完整性约束:1451
- spring - 一旦用户通过身份验证并且在授权他使用端点之前,更新 Authentication 对象中的权限
- swift - RealityKit 和 ARKit – 应用启动时 AR 项目在寻找什么?
- php - PHP output_buffering 选项
- javascript - 在 React 中插入映射的 SVG
- php - 如何在 symfony 中为我的 http 请求配置 base_uri?
- keras - 异常具有与正常数据相似的误差值
- python - 如何绘制这个有很多功能的图的图例?
- php - 无法在 Docker 容器中安装依赖于 GD PHP 扩展的作曲家包