Robert Stevens
Robert Stevens

Reputation: 8677

NodeJs Crypto - MD5 Hash is incorrect

I have a php example and trying to recreate it in NodeJs using Crypto:

https://docs.paygate.co.za/?php#request

My code:

var encryptionKey = "secret";

  var data = {
    PAYGATE_ID: 10011072130,
    REFERENCE: "pgtest_20200713124029",
    AMOUNT: 100,
    CURRENCY: "ZAR",
    RETURN_URL: "https://google.com",
    TRANSACTION_DATE: "2020-07-13 12:40:29",
    LOCALE: "en",
    COUNTRY: "ZAF",
    EMAIL: "[email protected]",
  };

  var CHECKSUM = crypto
    .createHash("md5")
    .update(JSON.stringify(data) + encryptionKey)
    .digest("hex");

The checksum they are expecting:

e7d0f0d8e7066c968a5a2396cdea0c8f

The checksum my code is generating:

2f3b1a8c8064b6bd58ee1d841e1c8050

From the documentation they expect me to append the secret at the end of the string. Which I am doing.

I am not sure why my checksum is incorrect, any tips?

Upvotes: 3

Views: 1362

Answers (1)

Jiří Pospíšil
Jiří Pospíšil

Reputation: 14412

JSON.stringify is not an equivalent of implode, have a look at the resulting values. I think the closest thing you can do in JavaScript is Object.values(data).join("").

  var CHECKSUM = crypto
    .createHash("md5")
    .update(Object.values(data).join(""))
    .update(encryptionKey)
    .digest("hex");

Upvotes: 3

Related Questions