1.1.1 • Published 2 years ago
ts-totp v1.1.1
ts-totp
Description
Generate a dynamic password and verify that the password is valid within a specified time
Installation
- yarn add ts-totp
Description
// totp使用说明
import { getToken, validate } from 'ts-totp';
/**
* @param {string} secret 密钥.
* @param {number} [config.digits=6] 生成的token长度.
* @param {number} [config.interval=60] 口令有效期,单位秒,默认60秒.
*/
// 生成动态口令
const token = getToken({ secret: 'K33DFSsasdassssVS', digits: 10, interval: 60 });
// token = 1950901855
// 校验口令
const res = validate(token, { secret: 'K33DFSsasdassssVS', digits: 10, interval: 60 });
// res = true
// 携带参数校验使用说明
import { getCode, validateCode } from 'ts-totp';
/**
* prefix string 前缀
* secret string 密钥
* digits number 验证码位数
* interval number 验证码有效时间
* num number 验证数字(自动查找其临近的质数)
* type string 质数查找方式(up/down)up:向上查找,小于该数的最大质数 down:向下查找,大于该数的最小质数
* data number 携带的信息,比如用户ID.数字要小于num。如:当前系统设计最大承受用户有96742人,则num可以设定必须大于96742的数值(96743)
*/
const config = { prefix: '10', secret: 'K33DFSsssssVS', digits: 10, interval: 60, num: 96743, type: 'down' };
// 生成带参口令
const params = Object.assign({ data: 1002 }, config);
const code = getCode(params);
// code = 129477686511008
// 校验口令
const res = validateCode(code, config);
//成功 res = { message: 'success', data: 1002, token: 1338284494 }
//失败 res = { message: 'fail' }
Contribution
- Fork the repository
- Create Feat_xxx branch
- Commit your code
- Create Pull Request
Gitee Feature
- You can use Readme_XXX.md to support different languages, such as Readme_en.md, Readme_zh.md
- Gitee blog blog.gitee.com
- Explore open source project https://gitee.com/explore
- The most valuable open source project GVP
- The manual of Gitee https://gitee.com/help
- The most popular members https://gitee.com/gitee-stars/