Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Java MD5 MessageDigest without catching NoSuchAlgorithmException?

In Java, if I want to compute an MD5 sum, I need to be aware of possible exceptions:

try {
   MessageDigest md = MessageDigest.getInstance("MD5");
   // Do stuff
} catch (NoSuchAlgorithmException e) {
   // Can't happen...
}

However, according to the JVM spec, MD5 must be supported, so the exception should never be thrown. Is there a different access pattern that allows me to write more elegant code?

like image 711
MikeD Avatar asked Apr 03 '14 01:04

MikeD


1 Answers

Instead of MessageDigest you can use common.apache DigestUtils. This is easy to use and no need to take such long procedure to digest data like MessageDigest does.

DigestUtils.md5("String to digest");

Go through this Class and follow this documentation

like image 136
Harshal Patil Avatar answered Sep 28 '22 23:09

Harshal Patil