Manish Jangir
Manish Jangir

Reputation: 5437

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.

Upvotes: 4

Views: 4001

Answers (2)

dev-backend-tb
dev-backend-tb

Reputation: 1

I have the same problem, and i found solution using the packet: lcobucci/jwt.

Must to create a buffer by your key in base64, after create will be converting to binary for sign the jwt.

  $configuration = Configuration::forSymmetricSigner(
                // You may use any HMAC variations (256, 384, and 512)
                new Sha256(),
                // replace the value below with a key of your own!
                InMemory::base64Encoded('your-base64-key')
                // You may also override the JOSE encoder/decoder if needed by providing extra arguments here
            );

Upvotes: 0

KatsuoRyuu
KatsuoRyuu

Reputation: 321

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 ;)

Upvotes: 5

Related Questions