所以我必须登录一个网站,其中有TOTP代码。我已经做了一个简单的NodeJS脚本,它可以让我得到TOTP代码,但是它总是无效的。
var notp = require('notp');
var base32 = require('thirty-two');
var key = 'KEYHERE';
var token = notp.totp.gen(key, 30);
console.log(token);
var login = notp.totp.verify(token, key);
if (!login) {
return console.log('Token invalid');
}
console.log('Token valid, sync value is %s', login.delta);另外,我已经同步了我的时间(不确定我是否做对了)。谁能帮我解决这个代码或同步时间在服务器上。服务器来自法国
发布于 2016-11-28 13:32:54
您可以使用npm模块的时间同步示例:
// create a timesync instance
var ts = timesync({
server: '...', // either a single server,
peers: [...] // or multiple peers
});
// get notified on changes in the offset
ts.on('change', function (offset) {
console.log('offset from system time:', offset, 'ms');
}
// get the synchronized time
console.log('now:', new Date(ts.now()));发布于 2016-11-28 15:54:37
您是否生成并验证了notp
var notp = {};
notp.gen = function(key, opt) {
opt = opt || {};
var time = opt.time || 30;
var _t = Date.now();
// Time has been overwritten.
if(opt._t) {
if(process.env.NODE_ENV != 'test') {
throw new Error('cannot overwrite time in non-test environment!');
}
_t = opt._t;
}
// Determine the value of the counter, C
// This is the number of time steps in seconds since T0
opt.counter = Math.floor((_t / 1000) / time);
return hotp.gen(key, opt);
};
notp.verify = function(token, key, opt) {
opt = opt || {};
var time = opt.time || 30;
var _t = Date.now();
// Time has been overwritten.
if(opt._t) {
if(process.env.NODE_ENV != 'test') {
throw new Error('cannot overwrite time in non-test environment!');
}
_t = opt._t;
}
// Determine the value of the counter, C
// This is the number of time steps in seconds since T0
opt.counter = Math.floor((_t / 1000) / time);
return hotp.verify(token, key, opt);
};
module.exports.totp = totp;https://stackoverflow.com/questions/40837598
复制相似问题