4

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.

3
  • have you found the solution yet? Commented Jun 6, 2018 at 11:26
  • No could not found yet.. Commented Jun 7, 2018 at 15:27
  • This Q&A needs review. The question which drew me here is about a NodeJS Buffer equivalent in PHP. The Buffer library does a lot more than Base64 operations. The responses focus on the single example of Base64 operations. So, was the OP actually looking for an equivalent of Buffer? Or was the OP just looking for Base64 manipulation. I suspect there might still not be an answer for the former, but lots of answers for the latter if the question is properly asked. Commented Sep 8, 2021 at 22:24

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Consider adding the function results and better examples.

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.