Search code examples
javascriptcryptojscybersource

Anyone gotten CryptoJS to work with Cybersource REST API v3 reporting for the Signature?


Unable to generate correct HMAC 256 hash for Cybersource Signature headers

I have been working on this for a couple of days, and I can easily get it to work within .Net using the supplied sample code to generate the correct HMAC Signature. However I cannot get CryptoJS to work, I beleive it stems from the fact that CryptoJS is interpreting the "\n" as a LRCF internally and thus throws the encryption off. Please be aware that I am limited to ECMA5 and have brought the CryptoJS in as a minified function.

var data = "host: api.cybersource.com\ndate: Mon, 10 Jun 2019 20:41:05 GMT\n(request-target): get /reporting/v3/report-downloads?organizationId={OrgId}&reportDate=2019-06-06&reportName=PaymentBatchDetailReport\nv-c-merchant-id: {MerchId}";

var hash   = CryptoJS.HmacSHA256(data, "{SecretKey}");
var base64 = CryptoJS.enc.Base64.stringify(hash);
document.write(base64);

Any help would be greatly appreciated!


Solution

  • Make sure to Base64 decode your secret key before passing it to CryptoJS.HmacSHA256.

    For example var words = CryptoJS.enc.Base64.parse({SecretKey}); var hash = CryptoJS.HmacSHA256(data, words);