How to Generate a SHA-256 Encrypted Hashvalue

Document created by sheng_liao462475 Employee on Nov 16, 2016Last modified by Adam Arrowsmith on Dec 2, 2016
Version 12Show Document
  • View in full screen mode

The following approach can be used to generate a SHA-256 Encrypted hashvalue within your process. There are two methods -the first method will generate the output in Base 64, the second method will generate the output in Hex.

 

1. In your process, it is recommended to use a Dynamic Process Property "input" to store the value that needs to be encrypted. This property is then referenced in the script below.

 

2. Use the below script to generate a SHA-256 encrypted hashvalue.

 

To generate the output in Base64:

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("input");

MessageDigest mac = MessageDigest.getInstance("SHA-256");
byte[] signatureBytes = mac.digest(str.getBytes(StandardCharsets.UTF_8));
encryptedSignature = new String(DatatypeConverter.printBase64Binary(signatureBytes));
ExecutionUtil.setDynamicProcessProperty("output", encryptedSignature, false);

dataContext.storeStream(is, props);
}

 

To generate the output in HEX string:

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("input");
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();
ExecutionUtil.setDynamicProcessProperty("output", encryptedSignature, false);

dataContext.storeStream(is, props);
}

Attachments

    Outcomes