title: JavaScript encrypt in sha512 and obtain result in raw binary data like PHP hash()
body: <p>As I said in the title I need to reproduce this PHP function (in JavaScript):</p>
  <pre class="lang-php prettyprint-override"><code>hash(&quot;sha512&quot;, &quot;string&quot;, true);
  </code></pre>
  <p>The tricky part if for the true at the end which means:</p>
  <blockquote>
  <p>When set to TRUE, outputs raw binary data. FALSE outputs lowercase
  hexits.</p>
  </blockquote>
  <p>This can be important too if I need to create something by myself and if I should take care of the endian:</p>
  <blockquote>
  <p>The tiger algorithm now uses big-endian byte ordering.</p>
  </blockquote>
  <p>Reference: <a href="http://php.net/manual/en/function.hash.php" rel="nofollow noreferrer">php.net - hash()</a></p>
  <p><strong>EDIT</strong>: After some tests and research here is the function I need to reproduce in JavaScript:</p>
  <pre class="lang-php prettyprint-override"><code>hex2bin(&quot;string&quot;);
  </code></pre>
  <p>Actually witch CryptoJS I'm able to obtain the result in hexa. I don't know exactly how to obtain it in row binary data (hex2bin equivalent).</p>
  <p>I found that with NodeJS and the library crypto we can do that easily but I don't use NodeJS. I need to obtain the same result in classic JavaScript (or jQuery or any other lib which will work).</p>
  <p>My goal is to send to a Symfony's WSSE protocol a password that will match the one in database (passwordDigest).</p>
  <p>Here is the PHP code I need to reproduce:</p>
  <pre class="lang-php prettyprint-override"><code>public function encodePassword($raw, $salt)
      {
  
          // ... Some check code for data
  
          $salted = $this-&gt;mergePasswordAndSalt($raw, $salt); // Just do $raw.&quot;{&quot;.$salt.&quot;}&quot;;
          $digest = hash($this-&gt;algorithm, $salted, true);
  
          // &quot;stretch&quot; hash
          for ($i = 1; $i &lt; $this-&gt;iterations; $i++) {
              $digest = hash($this-&gt;algorithm, $digest.$salted, true);
          }
  
          return $this-&gt;encodeHashAsBase64 ? base64_encode($digest) :
  bin2hex($digest);
      }
  </code></pre>
  <p>And here is the code in JavaScript I actually have:</p>
  <pre class="lang-js prettyprint-override"><code>WSSE.encodePassword = function(password, salt) {
      try {
          var salted = WSSE.mergePasswordAndSalt(password, salt);
          var digest = CryptoJS.SHA512(salted).toString();
  
          for (i = 1; i &lt; 5000; i++) {
              digest = CryptoJS.SHA512(digest + salted).toString();
          }
  
          return WSSE.base64_encode(digest);
      } catch(err) {
          Log.e(&quot;WSSE&quot;, err.message);
          return null;
      }
  };
  </code></pre>
  <p>The problem with this code is that the digest variable always contains data in hexa, not in raw binary data.</p>
  <p>An idea, code, or lib everyone?</p>