Saturday, May 25, 2024
 Popular · Latest · Hot · Upcoming
137
rated 0 times [  139] [ 2]  / answers: 1 / hits: 28026  / 12 Years ago, thu, august 23, 2012, 12:00:00

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

More From » php

 Answers
134

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/


[#83456] Wednesday, August 22, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elliem

Total Points: 415
Total Questions: 117
Total Answers: 94

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
;