Toggle navigation
Toggle navigation
This project
Loading...
Sign in
tools
/
totp-key
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
lintry
2017-06-22 14:54:47 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
87bb8fc528a89b0d97b2c8b7b14462b3ad268fe1
87bb8fc5
1 parent
6425f197
调整生成密钥方法
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
30 deletions
lib/totp.js
test/authenticator.test.js
lib/totp.js
View file @
87bb8fc
...
...
@@ -2,42 +2,46 @@
* Created by lintry on 2017/5/19.
*/
const
TOTP
=
function
(
options
)
{
if
(
!
(
this
instanceof
TOTP
))
{
return
new
TOTP
(
options
)
}
const
_
=
require
(
'lodash'
),
const
_
=
require
(
'lodash'
),
crypto_utils
=
require
(
'kml-crypto-utils'
),
speakeasy
=
require
(
'speakeasy'
),
Authenticator
=
require
(
'./authenticator'
);
options
=
options
||
{};
/**
* 生成密钥并加密
* @return {*}
*/
const
createSecret
=
function
()
{
return
crypto_utils
.
AESEncode
(
speakeasy
.
generateSecret
({
length
:
20
}).
base32
);
};
const
TOTP_OPTIONS
=
{
encoding
:
options
.
encoding
||
'base32'
,
step
:
options
.
step
||
30
,
algorithm
:
options
.
algorithm
||
'sha512'
};
const
TOTP
=
function
(
options
)
{
if
(
!
(
this
instanceof
TOTP
))
{
return
new
TOTP
(
options
)
}
/**
* 生成密钥并加密
* @return {*}
*/
this
.
createSecret
=
function
()
{
return
crypto_utils
.
AESEncode
(
speakeasy
.
generateSecret
({
length
:
20
}).
base32
);
};
/**
* 解析密钥
* @param secret
* @return {Authenticator}
*/
this
.
parse
=
function
(
secret
)
{
return
new
Authenticator
(
crypto_utils
.
AESDecode
(
secret
),
TOTP_OPTIONS
)
};
return
this
;
options
=
options
||
{};
const
TOTP_OPTIONS
=
{
encoding
:
options
.
encoding
||
'base32'
,
step
:
options
.
step
||
30
,
algorithm
:
options
.
algorithm
||
'sha512'
};
/**
* 解析密钥
* @param secret
* @return {Authenticator}
*/
this
.
parse
=
function
(
secret
)
{
return
new
Authenticator
(
crypto_utils
.
AESDecode
(
secret
),
TOTP_OPTIONS
)
};
return
this
;
};
TOTP
.
createSecret
=
createSecret
;
TOTP
.
prototype
.
createSecret
=
createSecret
;
module
.
exports
=
TOTP
;
\ No newline at end of file
...
...
test/authenticator.test.js
View file @
87bb8fc
...
...
@@ -7,6 +7,8 @@ const chalk = require('chalk');
const
fs
=
require
(
'fs-extra'
);
const
path
=
require
(
'path'
);
console
.
log
(
chalk
.
magenta
(
'new secret '
+
TOTP
.
createSecret
()));
//使用外部生成的密钥
// let secret = 'vH6OdbUEjSukTqlDvW3TYdusjiOIkxRnAHNTjJewfZa5yNueG9wx1N9pJMFOmPAV';
let
secret
=
'acnahbfX3bKa+EuhD7sR+MToc8d5rZ8Db7xh68ZUnBX3SPfhlHS/GeJ7SKEfcLTI'
;
...
...
Please
register
or
login
to post a comment