Unable to find browser js code for nodejs crypto code

212 views Asked by At

I have a nodejs code which generate signature based on current time stamp:

var crypto = require('crypto');
var nonce = new Date().getTime();
var signature = function(nonce, secretKey) {
    var signature = crypto.createHmac('sha256', Buffer.from(secretKey, 'utf8')).update(Buffer.from(nonce, 'utf8')).digest('base64');
    return signature;
}

I am converting this code into browser js but i did not succeed in my task. I took reference from this link: nodejs crypto module vs crypto-js but did not work.

if nonce=1518440585425 and secret=9IeVABv94EQBnT6Mn73742kBZOmzFpsM+c62LU9b/h4= it should give this signature=blI2ILR8MW4ParkT4R1vvVOXF42gJOAVPgEJtZT7Ivo=

1

There are 1 answers

0
Ajit Soman On BEST ANSWER

Your nodejs signature generation code will be equivalent to this crypto js code:

var secret="9IeVABv94EQBnT6Mn73742kBZOmzFpsM+c62LU9b/h4=";
var nonce = "1518440585425";
var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256,secret);
hmac.update(nonce);
var hash = hmac.finalize();
var signature = hash.toString(CryptoJS.enc.Base64);

console.log(signature); //blI2ILR8MW4ParkT4R1vvVOXF42gJOAVPgEJtZT7Ivo

You will need to include these js in your HTML file:

<script src="components/core.js"></script>
<script src="components/hmac.js"></script>
<script src="components/sha256.js"></script>
<script src="components/enc-base64.js"></script>