0

I am pretty new to Node.

I am trying to get the following PHP code

$signature = base64_encode(hash_hmac('sha256', $canonicalString, $secret, true));
$signature = rtrim($signature,"=");

to work in node . Currently I have the following code implementation in node.

  return crypto.createHmac('sha256', key)
    .update(message)
    .digest('base64');

Still the service not getting authenticated.

Any help is much appreciated. Been stuck at the same line of code for more than a day.

Thank you

Sammitch
  • 30,782
  • 7
  • 50
  • 77
LReddy
  • 117
  • 17
  • Looks like all you're missing is something to trim any `=` off the end. – Sammitch Jan 18 '20 at 02:24
  • I even tried the trim. still didn't work. After string is retuned Signature = Signature.substring(0, Signature.length - 1);. Still getting the same error. – LReddy Jan 18 '20 at 02:27
  • Apart from trimming, there seems to be no functional difference. Base64 encoded strings can have no, one or two padding characters (`=`). Try trimming with `replace(/=+$/g,'')`, [here](https://stackoverflow.com/a/32516190). Authentication may also fail for another reason. – Topaco Jan 18 '20 at 08:49
  • Show us example inputs and outputs. – Sammitch Jan 18 '20 at 23:47

1 Answers1

0

Sorry for the late update. the following code worked for me.

return crypto.createHmac('sha256', key)
    .update(message)
    .digest('base64');

The mistake i did was using different keys in the PHP code and the node code.

LReddy
  • 117
  • 17