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

javascript array
explode javascript
php list
javascript multiple assignment
javascript destructuring
javascript array assignment
javascript tuple unpacking
javascript destructor

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

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 for this. If you didn't include this, CryptoJS.enc.Base64 will be undefined and fallback to the default.

Working demo:

Is their a JavaScript Equivalent to PHP explode()?, The JavaScript equivalent to PHP explode() is split(). To get the data after the first colon, try to run the following code.Example < Javascript equivalent of PHP's list() Ask Question Asked 10 years, 5 months ago. Active 6 months ago. Viewed 35k times 59. 7. Really like that function.

php code

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

php output


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


PHP's empty in JavaScript, Here's what our current JavaScript equivalent to PHP's empty looks like. discuss at: // original by: Philippe Baumann // input by:  What is the perfect equivalent of PHP’s isset in JavaScript? In a broader sense, a general, complete guide on JavaScript’s handling of variables that don’t exist, variables without a value, etc. would be convenient.


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

PHP's explode in JavaScript, Here's what our current JavaScript equivalent to PHP's explode looks like. discuss at: // original by: Kevin van Zonneveld  JavaScript Equivalent of PHP’s ucwords Function. The JavaScript function below performs the equivalent of PHP’s strtolower and then ucwords function combined on the text inside a given text input element. It’s straight JavaScript and doesn’t require jQuery.

You can also use this npm package to do the same in Javascript.

var jsSHA = require('jssha');

hmac_sha1(string, key){
    let shaObj = new jsSHA("SHA-1", "TEXT");
    shaObj.setHMACKey(key, "TEXT");
    let hmac = shaObj.getHMAC("B64");
    return hmac;

Javascript Equivalent of PHP List(), Equivalent of PHP Class Traits in JavaScript. Mar 2019. I'm currently working on a fairly JS-heavy project called Livewire. I'm not sure I've ever written more raw  This is one such heck of a question. These are two different platforms that aren’t really equivalent to one another. The only one similarity is both PHP and Node.js is for back-end.

This worked for me :

var CryptoJS = require("crypto-js");
const raw_signature = hmacSHA1(baseString, signingKey);
const signature = raw_signature.toString(CryptoJS.enc.Base64);

It's giving the exact same result as, in PHP, :

$rawSignature = hash_hmac("sha1", $baseString, $signingKey, true);
$signature    = base64_encode($rawSignature);

Equivalent of PHP Class Traits in JavaScript, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  Yes, the newbiest of questions, tis true, but what is the JS equivalent of PHPs Echo command? I know about using the Alert box, but thats not what I want. I just want a simple Hello World on the page, not as a pop up. Now, I also know about document.write, too..BUT w3schools page on this specific

JavaScript String trim() Method, JavaScript Equivalent of PHP include. I would like to know if anyone knows the equivalent way to call on pieces of HTML code from another file  If you are familiar with PHP’s isset() function, you know that it is used to determine if a variable has been set and is not NULL. JavaScript isset() Equivalent

JavaScript Equivalent of PHP include, dump() - Javascript equivalent of PHP's print_r() function. The moment I saw the print_r() function of PHP, I fell in love with it. It is a very necessary function and I  What is the JavaScript Equivalent of PHP print_r() function? In other words, how you can “print” a javascript object in a way that’s readable by humans? Code. You could use JSON.stringify, as following: The HTML part. Let’s create two links for demo:

dump() - Javascript equivalent of PHP's print_r() function, PHP Is not fair to compare PHP vs JavaScript, as they both have different purposes for web-site development. PHP is a server-side scripting language while  Onload is used by JavaScript but is not a part of JavaScript itself - it is a hook into the DOM. Conceptually, onload can only exist in a scripting language run in a browser. JavaScript is the only widely available "in browser" language. Others exist, but they are really more experimental than anything.

  • I have already tried that, it doesn't seem to work. Please see my revised question for a better explanation.
  • @Kevin did you include the base64-min.js? Since it works fine here:
  • @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
  • 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!
  • @Esailija I think the fiddle on longer works. Looks like Google moved those scripts
  • Your code is not clear, what is the key and what is the value?