【JS 第三方库】authenticator

2024-02-20 00:00:00

目录:

authenticator 模块介绍

authenticator 用于实现双因素身份验证(2FA) 或一次性密码(OTP)功能的库。
这类模块的核心是基于 TOTP(Time-Based One-Time Password) 或 HOTP(HMAC-Based One-Time Password) 算法,生成与时间同步的动态验证码,从而增强应用的安全性。

安装依赖:

$ npm install authenticator --save

TOTP 示例

import authenticator from 'authenticator'
 
// 生成密钥
const secret = authenticator.generateKey()
console.log('secret', secret) // 'nwz5 ebpu dh65 awj7 imsf zfeu 6gpq xwsu'
 
// 生成 TOTP token
const token = authenticator.generateToken(secret)
console.log('token', token) // '031624'
 
// 验证 token,验证失败 result = null,验证成功 result = { delta: 0 }
const result = authenticator.verifyToken(secret, '000000')
console.log('result', result) // { delta: 0 }

返回首页

本文总阅读量  次
皖ICP备17026209号-3
总访问量: 
总访客量: