Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi all,
Is there an easy way to calculate a hash-key in a route? I know there is a component in jobs to do that, but I don't want to use a job in my route.
I'm working with messages from a queue (ActiveMQ), so there will be all kinds of structures of messages to calculate a hash for.
I would like to have a route like this:
Input JMS -> Hash calculation of message body -> Output JMS
I hope this is possible and I'm looking forward to read the possibilities.
Hi,
Thanks for replying.
I fixed it using function DigestUtils.sha256Hex() in a cProcessor component.
Hello,
You are able to create a custom bean with the hashing code and just call it in your route.
package beans;
import java.io.IOException;
import java.io.InputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* Generates a hash value based on the provided inputStream.
*
* For example returns a sha-256 hash of a file stream.
*
*/
public class HashGenerator {
public static String DEFAULT_HASH_ALGORYTHM = "SHA-256";
protected String hashAlgo;
public HashGenerator(String hashAlgorithm) throws NoSuchAlgorithmException {
this.hashAlgo = hashAlgorithm;
// Test if algorithm exists
MessageDigest.getInstance(hashAlgo);
}
public HashGenerator() throws NoSuchAlgorithmException {
this(DEFAULT_HASH_ALGORYTHM);
}
public String checksum(InputStream stream) throws IOException, NoSuchAlgorithmException {
if (stream == null) {
return null;
}
MessageDigest md = MessageDigest.getInstance(hashAlgo);
// file hashing with DigestInputStream
try (DigestInputStream dis = new DigestInputStream(stream, md)) {
while (dis.read() != -1) ; //empty loop to clear the data
md = dis.getMessageDigest();
dis.close();
}
stream.close();
// bytes to hex
StringBuilder result = new StringBuilder();
for (byte b : md.digest()) {
result.append(String.format("%02x", b));
}
return result.toString();
}
}
Feel free to let us know if it is what you are looking for.
Best regards
Sabrina
Hi,
Thanks for replying.
I fixed it using function DigestUtils.sha256Hex() in a cProcessor component.
Hello,
Thanks for sharing your solution with us and feel free to let us know if there is any further help we can give.
Best regards
Sabrina
Using the Hashify API would also be an option I guess.