Various HMACs generated by nodejs and php

// base64-encode the binary result of the HMAC computation
$merchantSig = base64_encode(hash_hmac('sha256',$signData,pack("H*" , $hmacKey),true));

      

Above is the php code that generates the digest.

let h = crypto.createHmac('sha256', hmacKey).update(keyString).digest('base64');

      

The above nodejs code that generates the digest. The key I am using is hex in both php and node. What should I do differently in node to get the same result as in php. I know php uses a different encoding than nodejs. But what else am I missing here?

+3


source to share


2 answers


well there is no difference between hmac in both php and node.js.

this is normal behavior for the two pieces of code you provided.

in your php you are packing

yours$hmacKey

a step that doesn't exist in your node side;


in all the following examples i will use as hmac key and data string 123456

yello

eg:

php without packaging:

$merchantSig = base64_encode(hash_hmac('sha256',$signData, $hmacKey,true));
echo $merchantSig; // output : gKjrFq1nrRP33vGiAK9V1Z5bLX5EFZhcfy2flRIGPEI=

      

node.js without packaging:

let h = crypto.createHmac('sha256', hmacKey).update(keyString).digest('base64');
console.log(h); // output : gKjrFq1nrRP33vGiAK9V1Z5bLX5EFZhcfy2flRIGPEI=

      


now package both:



php with packing:

$merchantSig = base64_encode(hash_hmac('sha256',$signData,pack("H*" , $hmacKey),true));
echo $merchantSig; // output : Y8D5crzxQfFkwQn1OJHeZTS1KVuTH0y7qLuxyetE0TY=

      

node.js with packaginghere is the trick

var h = crypto.createHmac('sha256', hmacKey.packHex()).update(keyString).digest('base64');
//                                          ^^^^^^^^^
console.log(h); // output : Y8D5crzxQfFkwQn1OJHeZTS1KVuTH0y7qLuxyetE0TY=

      


Update

Here are some online tests for both php and nodejs in two cases (using a package without using a package)

php: https://3v4l.org/HCt4g

nodejs: http://rextester.com/YNNWN69327

Here are some more tests with different keys and strings. for php:

https://3v4l.org/JKdNk

and node.js

http://rextester.com/RXGM49887 ,

+1


source


Can you please support me. On nodejs version 8.10. Cryptotek is a decoded lie



0


source







All Articles