简体   繁体   English

jsSha HMAC与加密不匹配-OTP算法

[英]jsSha HMAC not match with crypto - OTP algorithm

This 2 codes doesn't return the same. 这2个代码不会返回相同的代码。 Sorry I am no expert of both library. 抱歉,我不是这两个图书馆的专家。

const jsSHA = require("jssha");

const time = "00000000030f7141"
const key = "101010"
var shaObj = new jsSHA("SHA-1", "HEX");
shaObj.setHMACKey(key, "HEX");
shaObj.update(time);
const hmac = shaObj.getHMAC("HEX");
console.log(hmac) 
// returns '536d6eed86796085f8ec2ead742c52fd73995f27'
---------------
const crypto = require('crypto')

const time = "00000000030f7141"
const key = "101010"
crypto.createHmac('sha1', new Buffer(key, 
'HEX')).update(time).digest('HEX')
// returns '8a3df92d2a68b32b2b571a1b71bfea03556e0df4'

My point is to avoid to use an external lib for using OTP with Google Authenticator. 我的观点是避免使用外部库将OTP与Google Authenticator一起使用。 Best, 最好,

your nodejs update() is no different. 您的nodejs update()没什么不同。 you need to use hex there also. 您还需要在那里使用十六进制。

Attached a sample code 附上示例代码

const jsSHA = require("jssha");

const time = "00000000030f7141"
const key = "101010"
var shaObj = new jsSHA("SHA-1", "HEX");
shaObj.setHMACKey(key, "HEX");
shaObj.update(time);
const hmac = shaObj.getHMAC("HEX");
console.log(hmac) 
// returns '536d6eed86796085f8ec2ead742c52fd73995f27'
const crypto = require('crypto')

let out = crypto.createHmac('sha1', new Buffer(key, 'hex')).update(new Buffer(time,'hex')).digest('hex')
// returns '536d6eed86796085f8ec2ead742c52fd73995f27'
console.log(out)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM