Skip to Content
0

SHA256 conversion in Groovy script to be used in HCI

Feb 07 at 08:00 AM

103

avatar image

Dear Experts,

We are trying to convert input string to SHA256 algorithm, this option is not available in standard conversion methods.

Can anyone help me with Groovy script on this?

This is what I tested online and it's working fine.

When I tried same in HCI I got the error. I am trying to put the input string body to be converted. Please suggest.

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
	def body = message.getBody( String input );
	def digest = java.security.MessageDigest.getInstance("SHA-256");
	digest.update( input.bytes );
	new BigInteger(1,digest.digest()).toString(16).padLeft(32, '0');
	return message;
}

Regards,

Thouheed

1.jpg (41.4 kB)
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Morten Wittrock
Feb 07 at 11:53 AM
0

Hi Thouheed

Some of the variables from the two code snippets got a bit jumbled up. The lines for getting the message body and updating the digest, should be something like the following:

def body = message.getBody(java.lang.String) as String // Gets the message body as a String
digest.update(body.bytes) // Updates the digest with the bytes of the message body

Regards,

Morten

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Hi Morten,

Thanks for your reply. I'm still not able to solve it.

This is the code I used in CPI:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.security.MessageDigest;
def Message processData(Message message) {
	
	def body = message.getBody(java.lang.String) as String;
	def digest  = java.security.MessageDigest.getInstance("SHA-256");
	digest.update(body.bytes);
	new BigInteger(1,body.digest()).toString(16).padLeft(32, '0');
	return message;
}


I'm getting the below error:

java.lang.NoSuchMethodException: No signature of method: java.lang.String.digest() is applicable for argument types: () values: []
Possible solutions: size(), next(), toList(), toSet(), toList(), next()

Regards,
Thouheed

0

Hi Thouheed

Change

new BigInteger(1,body.digest()).toString(16).padLeft(32, '0')

to

new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0')

Regards,

Morten

0