cancel
Showing results for 
Search instead for 
Did you mean: 

CPI- Generate SHA-256 HMAC using secret Key in groovy script.

ali_ismail8788
Discoverer
0 Kudos

Hello Experts,

I have requirement to generate a Secure SHA-256 HMAC of string in CPI Groovy script.

like below sample

string: Account=788&Password=12345&Sender=Sender1ℜceiver=010456789&Text=Test1.

Secret key: FD92A200F4A9EBCB4896177CA2760DD

i have tried the below code but i don't know how to pass secret key and if there's a better solution.

Thank you!!

MessageDigest digest = MessageDigest.getInstance("SHA-256");
encodedhash = digest.digest(AhashingValue.getBytes(StandardCharsets.UTF_8));
hexString = new StringBuffer();
for (int i = 0; i < encodedhash.length; i++) {
String hex = Integer.toHexString(0xff & encodedhash[i]);
if(hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
def AuthTokenFinal = hexString.toString()
message.setHeader("ASHA-256","${AuthTokenFinal}")

Accepted Solutions (0)

Answers (1)

Answers (1)

PriyankaChak
Active Contributor
0 Kudos

Hi,

You can store the secret key in security material and read the value from it and pass it to the script.

For the script, you can refer to the below blog post (Step 3 section).

https://blogs.sap.com/2021/07/04/cloud-integration-with-commerce-azure-blob-storage-using-rest-api/

Regards,

Priyanka

ali_ismail8788
Discoverer
0 Kudos

Hello Priyanka,

thank u !

i have tried the script that mentioned in the blog but got the below error.

Thanks .

PriyankaChak
Active Contributor
0 Kudos

Hi,

Are you sending the key in byte format like below?

def secretKey = clientsecret.decodeBase64()

Regards,

Priyanka