Javascript: Equivalent of PHP's hash_hmac() with RAW BINARY output?

23,982

Solution 1

This is explained in their documentation. Try this:

var hash = CryptoJS.HmacSHA256("Message", "Secret Passphrase");

var base64 = hash.toString(CryptoJS.enc.Base64);

You need to include http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64-min.js for this. If you didn't include this, CryptoJS.enc.Base64 will be undefined and fallback to the default.

Working demo: http://jsfiddle.net/ak5Qm/

Solution 2

PHP:

base64_encode(hash_hmac('sha256', $value, $key, true));

Nodejs equivalent:

const crypto = require('crypto');
let token = crypto.createHmac("sha256", key).update(value).digest().toString('base64');

Solution 3

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
Share:
23,982
Kevin Cooper
Author by

Kevin Cooper

React enthusiast currently working on mobile apps.

Updated on April 05, 2020

Comments

  • Kevin Cooper
    Kevin Cooper about 4 years

    I am connecting to the Amazon Product Advertising API, and to sign my request I need to base64-encode the raw binary output of an HMAC-SHA256 hash.

    In the PHP documentation for hash_hmac, the fourth parameter bool $raw_output controls whether the output is raw binary data (true) or lowercase hexits (false). My program works in PHP by simply setting that parameter to true.

    However, I am now trying to port this over to Javascript. I tried using the CryptoJS.HmacSHA256() function, but it seems to be returning the lowercase hexits. How can I convert this to binary?

    I have tried the following according to the CryptoJS documentation, but both outputs are identical:

    var hash = CryptoJS.HmacSHA256("hello", "key");
    console.log(hash.toString());
    console.log(hash.toString(CryptoJS.enc.Base64));
    
  • Kevin Cooper
    Kevin Cooper over 11 years
    I have already tried that, it doesn't seem to work. Please see my revised question for a better explanation.
  • Esailija
    Esailija over 11 years
    @Kevin did you include the base64-min.js? Since it works fine here: jsfiddle.net/ak5Qm
  • Kevin Cooper
    Kevin Cooper over 11 years
    Yes, I have included enc-base64.js from the "components" folder. Even without it there are no errors; is there a way for me to know whether it actually found it or not?
  • Esailija
    Esailija over 11 years
    @Kevin if you use google chrome, press ctrl+shift+j when your page loads and see if there are errors. You can also simply write CryptoJS.enc.Base64 in your console to see if it's undefined
  • Kevin Cooper
    Kevin Cooper over 11 years
    Like I said, there are no errors. However, the result of writing that IS undefined. Thank you, I'll look into what is causing the issue.
  • Esailija
    Esailija over 11 years
    Yeah I meant, in my chrome at least, the console is populated with error messages for resources (such as scripts) that were not found. Like this: i.imgur.com/j6NQI.png
  • Kevin Cooper
    Kevin Cooper over 11 years
    It turns out I had my scripts included in the wrong order: enc-base64 THEN hmac-sha256, rather than vice versa. Switching this order solved the problem. Thanks again!
  • tfont
    tfont about 10 years
  • D.Tate
    D.Tate almost 8 years
    @Esailija I think the fiddle on longer works. Looks like Google moved those scripts
  • Serdar D.
    Serdar D. over 6 years
    Your code is not clear, what is the key and what is the value?