I would like to convert this PHP code$a ^ $b(where length of a is 16, b is 32) to javascript code. Here is my implementation.
var xor = "";
for (var i = 0; i < a.length; i++) {
xor += String.fromCharCode(a.charCodeAt(i) ^ b.charCodeAt(i));
}
xor = b.substr(-16) + xor;
However, the result not the same. Please help me to figure it out. Thanks.
By the way, here's the part of code I'm working around:
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 < newchal.length; i++) {
xor += String.fromCharCode(newchal.charCodeAt(i) ^ newpwd.charCodeAt(i));
}
xor = newpwd.substr(-16) + xor;
The corresponding PHP code:
<?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;
?>
Where a and b are newchal and newpwd, respectively. And php.func()s come from http://phpjs.org/. The expected output is "821f984aa1062e56dbdc8f77454e5eb3".
f27eeb39d6695c32dbdc8f77454e5eb3xor7461746573740000000000000000000000000000000000000000000000000000==861f9f5ca51d5c32dbdc8f77454e5eb3- care to also post a working PHP version? :)