簡體   English   中英

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

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

這2個代碼不會返回相同的代碼。 抱歉,我不是這兩個圖書館的專家。

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'

我的觀點是避免使用外部庫將OTP與Google Authenticator一起使用。 最好,

您的nodejs update()沒什么不同。 您還需要在那里使用十六進制。

附上示例代碼

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