Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS Buffer Equivalent In PHP

NodeJS code:

const salt = new Buffer('GHlDHToiZA1ViUu+W+EXww==', 'base64');

Output like this:

<Buffer 18 79 43 1d 3a 22 64 0d 55 89 4b be 5b e1 17 c3>

I need the same output in PHP. Read somewhere about PHP's pack function but I don't know how to use it.

like image 594
Manish Jangir Avatar asked Oct 31 '25 22:10

Manish Jangir


1 Answers

Seems that you are working with base64; in php you are right pack and unpack is your friends.

example

in Node

$ node
> Buffer('hello world').toString('base64')
aGVsbG8gd29sZA==

in PHP

$ php -a
php > echo base64_encode('hello world');
aGVsbG8gd29ybGQ=

But if you are only looking for the binary:

in Node

> Buffer('hello wold')
<Buffer 68 65 6c 6c 6f 20 77 6f 6c 64>

in PHP

php > print_r(unpack('H*', 'hello world'));
Array
(
    [1] => 68656c6c6f20776f726c64
)

So in your instance you would first decode the base64 and then unpack it.

php > $raw = base64_decode('GHlDHToiZA1ViUu+W+EXww==');
php > print_r(unpack('H*', $raw));
Array
(
    [1] => 1879431d3a22640d55894bbe5be117c3
)

Easy peasy ;)

like image 173
KatsuoRyuu Avatar answered Nov 03 '25 13:11

KatsuoRyuu