views:

463

answers:

1

How would one calculate an SHA1 or MD5 hash within iReport at report execution? I need to compare a pre-calculated hash against a database driven field (string).

Using iReport 2.0.5 (Old) and Report Engine is embedded into a commercial application.

+1  A: 

I used iReport and Jasper Reports some years ago and I don't remember the details, but I remember that you could put in some way Java code to be evaluated. Using that feature you could calculate the MD5 in few lines:

String encryptionAlgorithm = "MD5";
String valueToEncrypt = "StackOverflow";
MessageDigest msgDgst = MessageDigest.getInstance(encryptionAlgorithm);
msgDgst.update(valueToEncrypt.getBytes(), 0, valueToEncrypt.length());
String md5 = new BigInteger(1, msgDgst.digest()).toString(16) ;
System.out.println(md5);

Need to import java.math.BigInteger, java.security.MessageDigest and java.security.NoSuchAlgorithmException;

To calculate SHA1 hash is almost the same:

String encryptionAlgorithm = "SHA-1";
String valueToEncrypt = "StackOverflow";
MessageDigest msgDgst = MessageDigest.getInstance(encryptionAlgorithm);
byte[] sha1hash = new byte[40];
msgDgst.update(valueToEncrypt.getBytes(), 0, valueToEncrypt.length());
sha1hash = md.digest();

Check this blog post about the creation of variables that can be evaluated at report run time http://www.eakes.org/77/java-injection-in-jasper-reports/

JuanZe
Thanks! I will definitely check this out.
Israel Lopez
Israel Lopez