Showing
2 changed files
with
18 additions
and
12 deletions
| ... | @@ -2,15 +2,24 @@ | ... | @@ -2,15 +2,24 @@ |
| 2 | * Created by lintry on 2017/5/19. | 2 | * Created by lintry on 2017/5/19. |
| 3 | */ | 3 | */ |
| 4 | 4 | ||
| 5 | +const _ = require('lodash'), | ||
| 6 | + crypto_utils = require('kml-crypto-utils'), | ||
| 7 | + speakeasy = require('speakeasy'), | ||
| 8 | + Authenticator = require('./authenticator'); | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * 生成密钥并加密 | ||
| 12 | + * @return {*} | ||
| 13 | + */ | ||
| 14 | +const createSecret = function () { | ||
| 15 | + return crypto_utils.AESEncode(speakeasy.generateSecret({length: 20}).base32); | ||
| 16 | +}; | ||
| 17 | + | ||
| 5 | const TOTP = function (options) { | 18 | const TOTP = function (options) { |
| 6 | if (!(this instanceof TOTP)) { | 19 | if (!(this instanceof TOTP)) { |
| 7 | return new TOTP(options) | 20 | return new TOTP(options) |
| 8 | } | 21 | } |
| 9 | 22 | ||
| 10 | - const _ = require('lodash'), | ||
| 11 | - crypto_utils = require('kml-crypto-utils'), | ||
| 12 | - speakeasy = require('speakeasy'), | ||
| 13 | - Authenticator = require('./authenticator'); | ||
| 14 | 23 | ||
| 15 | options = options || {}; | 24 | options = options || {}; |
| 16 | 25 | ||
| ... | @@ -21,14 +30,6 @@ const TOTP = function (options) { | ... | @@ -21,14 +30,6 @@ const TOTP = function (options) { |
| 21 | }; | 30 | }; |
| 22 | 31 | ||
| 23 | /** | 32 | /** |
| 24 | - * 生成密钥并加密 | ||
| 25 | - * @return {*} | ||
| 26 | - */ | ||
| 27 | - this.createSecret = function() { | ||
| 28 | - return crypto_utils.AESEncode(speakeasy.generateSecret({length: 20}).base32); | ||
| 29 | - }; | ||
| 30 | - | ||
| 31 | - /** | ||
| 32 | * 解析密钥 | 33 | * 解析密钥 |
| 33 | * @param secret | 34 | * @param secret |
| 34 | * @return {Authenticator} | 35 | * @return {Authenticator} |
| ... | @@ -40,4 +41,7 @@ const TOTP = function (options) { | ... | @@ -40,4 +41,7 @@ const TOTP = function (options) { |
| 40 | return this; | 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