Showing
4 changed files
with
56 additions
and
3 deletions
| 1 | { | 1 | { |
| 2 | - "name": "totp-key", | 2 | + "name": "kml-totp-key", |
| 3 | "version": "1.0.0", | 3 | "version": "1.0.0", |
| 4 | "description": "创建基于TOTP的登录密钥所需要的方法", | 4 | "description": "创建基于TOTP的登录密钥所需要的方法", |
| 5 | "main": "index.js", | 5 | "main": "index.js", |
| ... | @@ -23,5 +23,10 @@ | ... | @@ -23,5 +23,10 @@ |
| 23 | "lodash": "^4.17.4", | 23 | "lodash": "^4.17.4", |
| 24 | "qr-image": "^3.2.0", | 24 | "qr-image": "^3.2.0", |
| 25 | "speakeasy": "^2.0.0" | 25 | "speakeasy": "^2.0.0" |
| 26 | + }, | ||
| 27 | + "devDependencies": { | ||
| 28 | + "otpauth": "^2.2.3", | ||
| 29 | + "otplib": "^4.0.6", | ||
| 30 | + "sjcl": "^1.0.6" | ||
| 26 | } | 31 | } |
| 27 | } | 32 | } | ... | ... |
| ... | @@ -8,8 +8,9 @@ const fs = require('fs-extra'); | ... | @@ -8,8 +8,9 @@ const fs = require('fs-extra'); |
| 8 | const path = require('path'); | 8 | const path = require('path'); |
| 9 | 9 | ||
| 10 | //使用外部生成的密钥 | 10 | //使用外部生成的密钥 |
| 11 | -let secret = 'vH6OdbUEjSukTqlDvW3TYdusjiOIkxRnAHNTjJewfZa5yNueG9wx1N9pJMFOmPAV'; | 11 | +// let secret = 'vH6OdbUEjSukTqlDvW3TYdusjiOIkxRnAHNTjJewfZa5yNueG9wx1N9pJMFOmPAV'; |
| 12 | -let authenticator = new TOTP({algorithm: 'sha512'}).parse(secret); | 12 | +let secret = 'acnahbfX3bKa+EuhD7sR+MToc8d5rZ8Db7xh68ZUnBX3SPfhlHS/GeJ7SKEfcLTI'; |
| 13 | +let authenticator = new TOTP({algorithm: 'sha1'}).parse(secret); | ||
| 13 | console.log(chalk.cyan('totp的secret')); | 14 | console.log(chalk.cyan('totp的secret')); |
| 14 | console.log(secret, authenticator.totp_options); | 15 | console.log(secret, authenticator.totp_options); |
| 15 | 16 | ... | ... |
test/otpauth.test.js
0 → 100644
| 1 | +/** | ||
| 2 | + * Created by lintry on 2017/5/19. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +//用到了eval函数,不能在小程序内使用 | ||
| 6 | +const OTPAuth = require('otpauth'); | ||
| 7 | + | ||
| 8 | +let totp = new OTPAuth.TOTP({ | ||
| 9 | + 'issuer': 'ACME', | ||
| 10 | + 'label': 'AzureDiamond', | ||
| 11 | + 'algorithm': 'SHA1', | ||
| 12 | + 'digits': 6, | ||
| 13 | + 'period': 30, | ||
| 14 | + 'secret': OTPAuth.Secret.fromB32('NB2W45DFOIZA') | ||
| 15 | +}); | ||
| 16 | + | ||
| 17 | +// Generate TOTP token | ||
| 18 | +let token = totp.generate(); | ||
| 19 | + | ||
| 20 | +// Validate TOTP token | ||
| 21 | +let delta = totp.validate({ | ||
| 22 | + 'token': token, | ||
| 23 | + 'window': 10 | ||
| 24 | +}); | ||
| 25 | + | ||
| 26 | +// Convert to Google Authenticator key URI | ||
| 27 | +// otpauth://totp/ACME:AzureDiamond?issuer=ACME&secret=NB2W45DFOIZA&algorithm=SHA1&digits=6&period=30 | ||
| 28 | +let uri = totp.toString(); | ||
| 29 | +console.log(uri, delta, token) | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
test/otplib.test.js
0 → 100644
| 1 | +/** | ||
| 2 | + * Created by lintry on 2017/5/19. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +//用到了eval函数,不能在小程序内使用 | ||
| 6 | + | ||
| 7 | +const authenticator = require('otplib/authenticator').default; | ||
| 8 | +console.log(authenticator) | ||
| 9 | + | ||
| 10 | +authenticator.options.algorithm = 'sha1' | ||
| 11 | +authenticator.options.encoding = 'base32' | ||
| 12 | +const secret = authenticator.generateSecret(); | ||
| 13 | + | ||
| 14 | +const token = authenticator.generate('FA7HM4ZZG5AF4JSVHQ4XOVC6KFGVAMRZ'); | ||
| 15 | + | ||
| 16 | +const isValid = authenticator.check(123456, secret); | ||
| 17 | + | ||
| 18 | +console.log(token, secret, isValid) | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment