Tried to convert the following PHP function to Node.js but both are showing different output. The out of node.js function should be equal to that of PHP. Below is the code I have tried. I have also attached the hexdump value.
PHP
function hextobin() {
$hexString = md5(''); //d41d8cd98f00b204e9800998ecf8427e
$length = strlen($hexString);
$binString = "";
$count = 0;
while ($count < $length) {
$subString = substr($hexString, $count, 2);
$packedString = pack("H*", $subString);
if ($count == 0) {
$binString = $packedString;
} else {
$binString .= $packedString;
}
$count += 2;
}
return $binString;
}
Output = ��ُ�� ���B~
Hexdump -C value of above output =
00000000 ef bf bd 1d ef bf bd d9 8f ef bf bd 04 ef bf bd |................|
00000010 20 ef bf bd ef bf bd ef bf bd 42 7e 0a | .........B~.|
0000001d
Node.JS
exports.hex2bin = (key) => {
const m = crypto.createHash('md5');
m.update(key);
const hexString = m.digest('hex');
let binString ="";
let length = hexString.length;
let count = 0;
while (count < length) {
const sub = hexString.substr(count, 2);
let packedString = Buffer.from(sub, "hex");
if (count === 0) {
binString = packedString;
} else {
binString += packedString;
}
count += 2;
}
return binString;
};
Output = ������� ���B~
Hexdump -C value of above output =
00000000 ef bf bd 1d ef bf bd ef bf bd ef bf bd ef bf bd |................|
00000010 04 ef bf bd ef bf bd 20 ef bf bd ef bf bd ef bf |....... ........|
00000020 bd 42 7e 0a |.B~.|
00000024
Any help will be appreciated.