1

I am trying to convert the following code from JAVA to Salesforce

JAVA:

        byte[] key =  BaseEncoding.base16().decode("wtv");

        SecretKeySpec signingKey = new SecretKeySpec(key, HMAC_SHA256_ALGORITHM);

        Mac mac = Mac.getInstance(HMAC_SHA256_ALGORITHM);
        mac.init(signingKey);

        byte[] rawHmac = mac.doFinal(data.getBytes(C_UTF8));


        return  BaseEncoding.base64().encode(rawHmac);

Salesforce

    private string calculateHMAC(string hmc, string signingString){

        Blob data = crypto.generateMac('HmacSHA256', Blob.valueOf(signingString), Blob.valueOf(hmc));

       return EncodingUtil.base64Encode(data);

}

My returning result is not the same, and I cant figure out why...

Is my conversion correct? What do you think I did wrong?

Edit: Sample Data:

HMC = AED317ECD56FB9E4AE3CA43AC95E2F33FF0287404067E9811C920E60C99C064C

singingString = currencyCode:merchantAccount:merchantReference:paymentAmount:sessionValidity:shipBeforeDate:shopperEmail:skinCode:CAD:AdyenTest:RA:199:2016-04-24T10:30:00Z:2016-04-24:[email protected]:FmDLArzH

expected result: C0j2GrWHFVq16AlF746Sk8c7QdyLDTn1hHslWV50D8A=

2
  • Do you have sample inputs and expected outputs that we could use for testing? Apr 20, 2016 at 22:36
  • @DanielBallinger I updated my question.
    – raym0nd
    Apr 20, 2016 at 23:29

1 Answer 1

3

Apparently I made a mistake in the conversion.

So here's the right conversion method.

The issue was that the HMC was a string representation of a HEX so I had to parse it down

Here's the correct method:

 private string calculateHMAC(string hmc, string signingString){
        Blob data = crypto.generateMac('HmacSHA256', Blob.valueOf(signingString), EncodingUtil.convertFromHex(hmc));
        return EncodingUtil.base64Encode(data);           
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .