Use Javascript to confirm a hash made in PHP?

By : michael

I need a way of creating a hash in PHP (crypt() or md5()) that can be replicated by Javascript.


  1. Create a hash of "hello world" using PHP (using prearranged private salt)
  2. Put the hash and the plain text in two hidden form fields
  3. Form gets submitted via ajax to a Node.js server
  4. Use Javascript to repeat the hashing on the plain text (again with private salt) and check if the hashes match

Is there a native function in JS or a common (reliable) 3rd party script that I can use to accomplish this? I'd rather not invoke PHP from the Node server.

By : michael

In the browser, you could use CryptoJS to create an HMAC (Hash-based Message Authentication Code). The challenge will be distributing your "prearranged private salt."

Here is the sample code from the CryptoJS documentation:

<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>
    var hash = CryptoJS.HmacMD5("Message", "Secret Passphrase");
    var hash = CryptoJS.HmacSHA1("Message", "Secret Passphrase");
    var hash = CryptoJS.HmacSHA256("Message", "Secret Passphrase");
    var hash = CryptoJS.HmacSHA512("Message", "Secret Passphrase");

On Node.js, use the Crypto module:

  .createHmac("sha256", "Secret Passphrase")

This video can help you solving your question :)
By: admin