title: Implement PHP string xor with JavaScript
body: <p>I would like to convert this PHP code<code>$a ^ $b</code>(where length of a is 16, b is 32) to javascript code. Here is my implementation.</p>
  
  <pre><code>    var xor = "";
      for (var i = 0; i &lt; a.length; i++) {
          xor += String.fromCharCode(a.charCodeAt(i) ^ b.charCodeAt(i));
      }
      xor = b.substr(-16) + xor;
  </code></pre>
  
  <p>However, the result not the same. Please help me to figure it out. Thanks.</p>
  
  <p>By the way, here's the part of code I'm working around:</p>
  
  <pre><code>    var secret = "secret";
      var password = "password";
      var chal = "2ba5565a539c57c1ce2356e218faa321";
      var hexchal = php.pack('H32', chal);
      var newchal, newpwd, pappassword;
  
      newchal = php.pack('H*', php.md5(hexchal + secret));
      newpwd = php.pack("a32", password);
      var xor = "";
      for (var i = 0; i &lt; newchal.length; i++) {
          xor += String.fromCharCode(newchal.charCodeAt(i) ^ newpwd.charCodeAt(i));
      }
      xor = newpwd.substr(-16) + xor;
  </code></pre>
  
  <p>The corresponding PHP code:</p>
  
  <pre><code>    &lt;?php
      $secret = "secret";
      $password = "password";
      $chal = "2ba5565a539c57c1ce2356e218faa321";
  
      $hexchal = pack ("H32", $chal);
      $newchal = pack ("H*", md5($hexchal . $secret));
  
      $newpwd = pack("a32", $password);
      $pappassword = implode ("", unpack("H32", ($newpwd ^ $newchal)));
      echo $pappassword;
      ?&gt;
  </code></pre>
  
  <p>Where a and b are newchal and newpwd, respectively. And php.func()s come from <a href="http://phpjs.org/" rel="nofollow">http://phpjs.org/</a>. The expected output is "821f984aa1062e56dbdc8f77454e5eb3".</p>