Phantr4x
Phantr4x

Reputation: 41

hash_hmac() with RAW Binary OUTPUT in JavaScript

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

Upvotes: 4

Views: 1513

Answers (1)

muhawo
muhawo

Reputation: 51

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

Upvotes: 1

Related Questions