JS bitwise XOR运算符的行为是否与PHP的运算符不同?我在JS和PHP中收到了相同操作的不同结果。
这是PHP代码。如果字符为XOR按位运算符^
,则最终数组包含完全结果。它与Node.js按位XOR:
$b = hex2bin('db6dc3f86800da4f7c19e643e8941ebb');
$md5raw = md5($b, TRUE);
/* for the sake of verification with JS - the result is the same */
$h = bin2hex($md5raw); // => b885730327999e8823d113161c3538d3
$string = "1234567";
$string .= "\x00";
$len = 16;
$result = '';
$arr = array();
for ($i = 0; $i < strlen($string); $i++) {
$result .= $string[$i] ^ $md5raw[$i % $len];
array_push($arr, bin2hex($result));
}
print_r(bin2hex($result)); // => 89b7403712afa988
Array
(
[0] => 89
[1] => 89b7
[2] => 89b740
[3] => 89b74037
[4] => 89b7403712
[5] => 89b7403712af
[6] => 89b7403712afa9
[7] => 89b7403712afa988
)
这是上面PHP代码的Node.js等价物。这是代码https://repl.it/JjPV/0的链接。但由于某种原因,它在该编译器中不起作用。但是它适用于本地Node.js安装。
这是一个没有函数的代码(为了节省空间;函数在repl.it
链接中)。按位的结果是不同的。为什么会这样?如何使它与PHP返回相同?
let b = hex2bin('db6dc3f86800da4f7c19e643e8941ebb');
let md5hex = md5(b); // => b885730327999e8823d113161c3538d3
let md5raw = new Buffer(md5hex, "hex").toString('binary'); // to raw binary like in PHP md5(val, TRUE);
let string = "1234567"
string += "\x00";
let len = 16;
let result = '';
let arr = [];
for (let i = 0; i < string.length; i++) {
result += string[i] ^ md5raw[i % len];
arr.push(bin2hex(result));
}
console.log(bin2hex(result)) // => 31313031313132353733
console.log(arr);
[ '31',
'313130',
'3131303131',
'313130313131',
'31313031313132',
'3131303131313235',
'313130313131323537',
'31313031313132353733' ]