首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NodeJS -生成无效的totp代码

NodeJS -生成无效的totp代码
EN

Stack Overflow用户
提问于 2016-11-28 13:18:51
回答 2查看 265关注 0票数 0

所以我必须登录一个网站,其中有TOTP代码。我已经做了一个简单的NodeJS脚本,它可以让我得到TOTP代码,但是它总是无效的。

代码语言:javascript
复制
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);

另外,我已经同步了我的时间(不确定我是否做对了)。谁能帮我解决这个代码或同步时间在服务器上。服务器来自法国

EN

回答 2

Stack Overflow用户

发布于 2016-11-28 13:32:54

您可以使用npm模块的时间同步示例:

代码语言:javascript
复制
// 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()));
票数 0
EN

Stack Overflow用户

发布于 2016-11-28 15:54:37

您是否生成并验证了notp

代码语言:javascript
复制
 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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40837598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档