Calculate SHA1 or MD5 hash in iReport

こ雲淡風輕ζ 提交于 2019-12-03 17:08:48

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/

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