NodeJS - 生成无效的totp代码

时间:2016-11-28 05:18:51

标签: node.js

所以我必须登录一个有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);

另外,我已经同步了我的时间(不确定我是否正确)。有人可以帮我修复此代码或在服务器上同步时间。服务器来自法国

2 个答案:

答案 0 :(得分:0)

您可以使用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()));

答案 1 :(得分:0)

您是否生成并验证了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;