Skip to content

Latest commit

 

History

History
167 lines (116 loc) · 3.61 KB

README_zh.md

File metadata and controls

167 lines (116 loc) · 3.61 KB

jsotp

jsotp是一个用来生成用来生成及验证一次性密码的js模块,一次性密码通常用来在web应用或者其他登录系统中作为二步验证或多步验证使用。

该模块基于 RFC4226 (HOTP:基于计数器的一次性密码算法)和 RFC6238(TOTP:基于时间的一次性密码算法)实现

示例

功能

  • 随机生成base32加密的字符串
  • 将base32加密后的字符串生成otpauth链接,可用来生成二维码
  • 创建可验证的HOTP对象
  • 验证HOTP密码
  • 创建可验证的TOTP对象
  • 验证TOTP密码

安装

npm install jsotp

模块

全部模块支持:

const jsotp = require('jsotp');

使用

基于时间的OTP

// import
const jsotp = require('jsotp');

// Create TOTP object
const totp = jsotp.TOTP('BASE32ENCODEDSECRET');
totp.now(); // => 432143

// Verify for current time
totp.verify(432143); // => true

// Verify after 30s
totp.verify(432143); // => false

基于计数器的OTP

// import
const jsotp = require('jsotp');

// Create HOTP object
const hotp = jsotp.HOTP('BASE32ENCODEDSECRET');
hotp.at(0); // => 432143
hotp.at(1); // => 231434
hotp.at(2132); // => 242432

// Verify with a counter
hotp.verify(242432, 2132); // => true
hotp.verify(242432, 2133); // => false

生成随机base32加密字符串

// import
const jsotp = require('jsotp');

// Generate
const b32_secret = jsotp.Base32.random_gen();

接口

param: length
type: int
default: 16
return: String
desc: the length of random base32 encoded string.
param: secret
type: string
return: TOTP
desc: generate TOTP instance.
return: String
desc: get the one-time password with current time.
param: totp
type: string
return: Boolean
desc: verify the totp code.
param: issuer
type: string
return: string
desc: generate url with TOTP instance
param: secret
type: string
return: HOTP
desc: generate HOTP instance.
param: counter
type: int
return: String
desc: generate one-time password with counter.
param: hotp
type: string
param: count
type: int
return: Boolean
desc: verify the hotp code.
param: issuer
type: string
return: string
desc: generate url with HOTP instance

开发

  • 克隆代码并安装依赖
git clone git@github.com:LanceGin/jsotp.git
npm install
  • src/文件夹中进行源码编写,执行下面命令将es6代码编译成es2015,命令会生成一个lib/本地文件夹。
npm run build
  • 单元测试
npm test