Search code examples
node.jscryptographynode-crypto

Problems with node.js crypto module


I am trying to create an HMAC for use in authentication with an API.

However, I'm running into some issues with the standard node crypto.

Looking at this example under the Authentication section:

HMAC_SHA256 ( 13916834993JJHlXeDcFM , 230664ae53cbe5a07c6c389910540729 )

Hashing these two values should give me a return value of: cdbf5cc64c70e1485fcf976cdf367960c2b28cfc28080973ce677cebb6db9681

However, when I use the crypto library I can't seem to replicate this:

crypto.createHash('sha256').update('13916834993JJHlXeDcFM').update('230664ae53cbe5a07c6c389910540729').digest("hex") 

returns

798134a33b4f8af61c85c07b692f907607b03a7b7298faff1f05ec6712006f9c

and

crypto.createHmac("sha256", '13916834993JJHlXeDcFM').update('230664ae53cbe5a07c6c389910540729').digest('hex');

returns

3a67e4899e1eae579b3191fdde04a19086cf50f71ee497274e23e413b52a0d00

Furthermore, the fact that these are both different is an issue as well. For some reason the createHmac(algorithm, key) method is not working for me.

crypto.createHmac("sha256", '13916834993JJHlXeDcFM').digest('hex');

Should return

1edcb16556338085d86656163314ded51d90f297d9c1a67d87d5b00b9fc44eb5

But returns instead:

01440fc92c9f2e4923b98cbe0202359b6d2a88ec8b5d54d1690718112f671084

I'm trying to debug an error I am receiving in authentication with using a library related to the API listed above, but seem to have run into a handful of errors with the crypto library that is preventing me from getting much useful investigation done.


Solution

  • In the API example, MD5 ( secret ) is the key and nonce + client + key is your data. So the correct syntax is:

    crypto.createHmac("sha256", '230664ae53cbe5a07c6c389910540729').update('13916834993JJHlXeDcFM').digest('hex');
    

    which outputs:

    cdbf5cc64c70e1485fcf976cdf367960c2b28cfc28080973ce677cebb6db9681