2

Code snippets:

php:

$shaVal = '59bc125840733ea828f42e276661b01e177f1414';

$enc = base64_encode(pack('H*', $shaVal));

echo $enc;

//prints => WbwSWEBzPqgo9C4nZmGwHhd/FBQ=

and in Javascipt I used buffer npm module

let Buffer = require('buffer').Buffer;

let shaVal = '59bc125840733ea828f42e276661b01e177f1414';

//function similar to php's pack() and returns binary data 
let bData = Buffer.from(shaVal, 'hex').toString();
console.log('bData ', bData)

//encode with base64
let val64 = Buffer.from(bData, 'binary').toString('base64');
console.log('base 64 encode ', val64)

//prints => Wf0SWEBzPv0o/S4nZmH9Hhd/FBQ=

How can I get the exact same output printed by php?

Note: Both options showing binary data as Y�X@s>�(�.'fa�

0

1 Answer 1

2

It's because PHP pack returns string, where as javascript buffer returns Array.

This answer might help : https://stackoverflow.com/a/41962257/3086531

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.