AnsweredAssumed Answered

How to generate a UTF8 encoded String with SHA256?

Question asked by ErikIvarsson3431 on Aug 1, 2018
Latest reply on Aug 3, 2018 by ErikIvarsson3431

Hi all, 

 

I am working with an authentication for a third party web service, as part of the authentication I shall provide a UTF8  encoded Hashed string with SHA256. I have received an example for generating this in C#: 

var validationstring = "MY_SECRET_STRING";

Byte[] inputBytes = Encoding.UTF8.GetBytes(validationstring);
Byte[] hashedBytes = new SHA256CryptoServiceProvider().ComputeHash(inputBytes);
var validationhashstring = BitConverter.ToString(hashedBytes).Replace("-", "");

Console.WriteLine("HASH: " + validationhashstring);

 

I have tried to implement the solution found here: How to Generate a SHA-256 Encrypted Hashvalue  but it does not work when I supply this value to the webservice, if I test with a simple C# program given the code above it works just fine. I have also included the "Replace" part in my code in the Groovy script as well. 

 

The one I am testing right now with looks like this: 

 

import java.util.Properties;
import java.io.InputStream;
import com.boomi.execution.ExecutionUtil;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import javax.xml.bind.DatatypeConverter;
import java.security.MessageDigest;

String encryptedSignature = "";
String str = "";

for( int i = 0; i < dataContext.getDataCount(); i++ ) {
InputStream is = dataContext.getStream(i);
Properties props = dataContext.getProperties(i);

str = ExecutionUtil.getDynamicProcessProperty("MY_STRING_TO_BE_HASHED_in");
MessageDigest mac = MessageDigest.getInstance("SHA-256");
byte[] signatureBytes = mac.digest(str.getBytes(StandardCharsets.UTF_8));

StringBuffer hexString = new StringBuffer();
for (int j=0; j<signatureBytes.length; j++) {
String hex=Integer.toHexString(0xff & signatureBytes[j]);
if(hex.length()==1) hexString.append('0');
hexString.append(hex);
}

encryptedSignature = hexString.toString();
String encryptHash = encryptedSignature.replace("-","");

encryptHash = encryptHash.toUpperCase();

ExecutionUtil.setDynamicProcessProperty("MY_STRING_HASHED_out", encryptHash, false);

dataContext.storeStream(is, props);
}

 

This is far from my area of expertise so any help here is highly appreciated  

Outcomes