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�

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
SFernando
  • 1,074
  • 10
  • 35

1 Answers1

2

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

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

bhar1red
  • 440
  • 3
  • 10