Jean Guérin
Jean Guérin

Reputation: 13

jsSha HMAC not match with crypto - OTP algorithm

This 2 codes doesn't return the same. 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. Best,

Upvotes: 1

Views: 251

Answers (1)

karthick
karthick

Reputation: 12176

your nodejs update() is no different. 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)

Upvotes: 1

Related Questions