0

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.

7
  • 2
    The output is pretty much meaningless, they are just placeholder glyphs. Pipe the result through hexdump -C and share the result for each here. Commented May 8, 2020 at 5:24
  • Any particular reason to re-implement hex2bin in the PHP core? Commented May 8, 2020 at 5:42
  • @Peter to support the function in php 4.0 Commented May 8, 2020 at 6:29
  • @Evert added hexdump -C value, please check. Commented May 8, 2020 at 6:30
  • There is something going on that you are not showing. The hexdump -C of the output of the php routine does not match your display. Commented May 8, 2020 at 9:28

1 Answer 1

2

In PHP a string has no intrinsic encoding. It's just a series of bytes. In Javascript strings are UTF-16.

To deal with something equivalent to a php string in node the Buffer class is already an array of unsigned 8 bit bytes. See the node manual on Buffer for this quote:

In Node.js, Buffer objects are used to represent binary data in the form of a sequence of bytes. Many Node.js APIs, for example streams and file system operations, support Buffers, as interactions with the operating system or other processes generally always happen in terms of binary data.

The Buffer class is a subclass of the Uint8Array class that is built into the JavaScript language. A number of additional methods are supported that cover additional use cases. Node.js APIs accept plain Uint8Arrays wherever Buffers are supported as well.

The Buffer.from utility function you are using will read the entire hex string in one go rather than converting each pair of hex characters.

Proof of concept:

let hex2bin = () => {
    let hexString = 'd41d8cd98f00b204e9800998ecf8427e';
    let packedString =  Buffer.from(hexString, "hex");
    return packedString;
};

process.stdout.write(hex2bin());

hexdump

$ node test.js | hexdump -C
00000000  d4 1d 8c d9 8f 00 b2 04  e9 80 09 98 ec f8 42 7e  |..............B~|
00000010

PHP Script (based on your original code):

function hextobin() {
    //
    //$hexString = 'd41d8cd98f00b204e9800998ecf8427e';
    $hexString = md5('');
    $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;
}
echo hextobin();

hexdump

$ php -f hex2bin.php | hexdump -C
00000000  d4 1d 8c d9 8f 00 b2 04  e9 80 09 98 ec f8 42 7e  |..............B~|
00000010
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.