Showing
2 changed files
with
36 additions
and
30 deletions
| ... | @@ -2,42 +2,46 @@ | ... | @@ -2,42 +2,46 @@ |
| 2 | * Created by lintry on 2017/5/19. | 2 | * Created by lintry on 2017/5/19. |
| 3 | */ | 3 | */ |
| 4 | 4 | ||
| 5 | -const TOTP = function (options) { | 5 | +const _ = require('lodash'), |
| 6 | - if (!(this instanceof TOTP)) { | ||
| 7 | - return new TOTP(options) | ||
| 8 | - } | ||
| 9 | - | ||
| 10 | - const _ = require('lodash'), | ||
| 11 | crypto_utils = require('kml-crypto-utils'), | 6 | crypto_utils = require('kml-crypto-utils'), |
| 12 | speakeasy = require('speakeasy'), | 7 | speakeasy = require('speakeasy'), |
| 13 | Authenticator = require('./authenticator'); | 8 | Authenticator = require('./authenticator'); |
| 14 | 9 | ||
| 15 | - options = options || {}; | 10 | +/** |
| 11 | + * 生成密钥并加密 | ||
| 12 | + * @return {*} | ||
| 13 | + */ | ||
| 14 | +const createSecret = function () { | ||
| 15 | + return crypto_utils.AESEncode(speakeasy.generateSecret({length: 20}).base32); | ||
| 16 | +}; | ||
| 16 | 17 | ||
| 17 | - const TOTP_OPTIONS = { | 18 | +const TOTP = function (options) { |
| 18 | - encoding: options.encoding || 'base32', | 19 | + if (!(this instanceof TOTP)) { |
| 19 | - step: options.step || 30, | 20 | + return new TOTP(options) |
| 20 | - algorithm: options.algorithm || 'sha512' | 21 | + } |
| 21 | - }; | ||
| 22 | 22 | ||
| 23 | - /** | 23 | + |
| 24 | - * 生成密钥并加密 | 24 | + options = options || {}; |
| 25 | - * @return {*} | 25 | + |
| 26 | - */ | 26 | + const TOTP_OPTIONS = { |
| 27 | - this.createSecret = function() { | 27 | + encoding: options.encoding || 'base32', |
| 28 | - return crypto_utils.AESEncode(speakeasy.generateSecret({length: 20}).base32); | 28 | + step: options.step || 30, |
| 29 | - }; | 29 | + algorithm: options.algorithm || 'sha512' |
| 30 | - | 30 | + }; |
| 31 | - /** | 31 | + |
| 32 | - * 解析密钥 | 32 | + /** |
| 33 | - * @param secret | 33 | + * 解析密钥 |
| 34 | - * @return {Authenticator} | 34 | + * @param secret |
| 35 | - */ | 35 | + * @return {Authenticator} |
| 36 | - this.parse = function (secret) { | 36 | + */ |
| 37 | - return new Authenticator(crypto_utils.AESDecode(secret), TOTP_OPTIONS) | 37 | + this.parse = function (secret) { |
| 38 | - }; | 38 | + return new Authenticator(crypto_utils.AESDecode(secret), TOTP_OPTIONS) |
| 39 | - | 39 | + }; |
| 40 | - return this; | 40 | + |
| 41 | + return this; | ||
| 41 | }; | 42 | }; |
| 42 | 43 | ||
| 44 | +TOTP.createSecret = createSecret; | ||
| 45 | +TOTP.prototype.createSecret = createSecret; | ||
| 46 | + | ||
| 43 | module.exports = TOTP; | 47 | module.exports = TOTP; |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -7,6 +7,8 @@ const chalk = require('chalk'); | ... | @@ -7,6 +7,8 @@ const chalk = require('chalk'); |
| 7 | const fs = require('fs-extra'); | 7 | const fs = require('fs-extra'); |
| 8 | const path = require('path'); | 8 | const path = require('path'); |
| 9 | 9 | ||
| 10 | +console.log(chalk.magenta('new secret ' + TOTP.createSecret())); | ||
| 11 | + | ||
| 10 | //使用外部生成的密钥 | 12 | //使用外部生成的密钥 |
| 11 | // let secret = 'vH6OdbUEjSukTqlDvW3TYdusjiOIkxRnAHNTjJewfZa5yNueG9wx1N9pJMFOmPAV'; | 13 | // let secret = 'vH6OdbUEjSukTqlDvW3TYdusjiOIkxRnAHNTjJewfZa5yNueG9wx1N9pJMFOmPAV'; |
| 12 | let secret = 'acnahbfX3bKa+EuhD7sR+MToc8d5rZ8Db7xh68ZUnBX3SPfhlHS/GeJ7SKEfcLTI'; | 14 | let secret = 'acnahbfX3bKa+EuhD7sR+MToc8d5rZ8Db7xh68ZUnBX3SPfhlHS/GeJ7SKEfcLTI'; | ... | ... |
-
Please register or login to post a comment