Calculate SHA1 or MD5 hash in iReport

送分小仙女□ 提交于 2019-12-05 02:41:01

问题


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:


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/



来源:https://stackoverflow.com/questions/1589996/calculate-sha1-or-md5-hash-in-ireport

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!