4

I have the php code to generate hash_hmac

key = base64_encode(hash_hmac('sha1',$public_key, $private_key,TRUE));

I've tried the CryptoJS library to solve it.

According to the documentation:

var public_key = 'msg',
    private_key = 'key';
var hash = CryptoJS.HmacSHA1(public_key, private_key)

I don't know how to set the Raw Output to Binary like set $raw_output to true in php.

Can anyone help me?

Thanks:)

Phantr4x
  • 41
  • 3
  • CryptoJS doesn't provide an encoder to get a "binary string". Why would you even need it? There are all sorts of problems when dealing with binary data in JavaScript. What's wrong with Hex (`hash.toString()`)? – Artjom B. Mar 06 '16 at 16:18
  • Did you finally got it in js? I have the same problem, and need the raw output. Thanks in advance – Hugo Jul 08 '19 at 12:17

1 Answers1

1

php code

echo base64_encode(hash_hmac('SHA1', 'shanghai', '0', true).'beijing');

php output

xvBv49PpaYvXAIfy3iOSDWNQj89iZWlqaW5n

node code

var crypto = require('crypto');
var buf1 = crypto.createHmac("sha1", "0").update("shanghai").digest();
var buf2 = Buffer.from('beijing');
console.log(Buffer.concat([buf1, buf2]).toString('base64'));    

node output

xvBv49PpaYvXAIfy3iOSDWNQj89iZWlqaW5n
muhawo
  • 51
  • 2