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:)

2
  • 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())? Commented Mar 6, 2016 at 16:18
  • Did you finally got it in js? I have the same problem, and need the raw output. Thanks in advance Commented Jul 8, 2019 at 12:17

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.