Generate MD5 hash of input file

Situation:

The MD5 would be used to generate a hash value of a text file (created by a text schema), which value then could be sent as a field in an additional text file.

The MD5 value of the contents of the generated text file would then be sent in a separate file to the customer via a SFTP TRANSFER.

We would like to know if you may have a plugin which generates the MD5 hash value of the file, and then the calculated value could be used as a field in a text file.

 

Solution:

 Use the plugin below that takes the input stream of a file source, generates the MD5 hash value, then sets the value to the "MD5Hash" context variable. You can then use the data mapper to pick up the MD5Hash value from context and map it to a field (target schema) of the additional text file. Then you can use a FTP target activity to send the file.

Start > File Source (text file created by text schema) > Custom Plugin (generate stream set to false) > Data Mapping > Text Schema > FTP Target > End

 

import java.security.MessageDigest;

MessageDigest md = MessageDigest.getInstance("MD5");
byte[] dataBytes = new byte[1024];
int nread = 0;
while ((nread = inputStream.read(dataBytes)) != -1) {
    md.update(dataBytes, 0, nread);
}
byte[] mdbytes = md.digest();
// convert the byte to hex format
StringBuffer sb = new StringBuffer();
for (int i = 0; i < mdbytes.length; i++) {
    sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
String md5Hash = sb.toString();
// log MD5 hash in process flow logs in INFO mode
service.getLogger().info("Digest (in hex format) :: " + md5Hash);
// set MD5 hash in process flow context
context.put("MD5Hash", md5Hash);

Have more questions? Submit a request

0 Comments

Article is closed for comments.