I need to compute MD5 hash of a String and googling I found this.
I want to have a reusable utility class responsible of hashing a String, that I would use like this: AeSimpleMD5.MD5(myString);.
This call implies I have to check for NoSuchAlgorithmException and UnsupportedEncodingException everytime I want to hash a String; I have a couple of question about this, I will copy the lines that may throw exeption:
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes("iso-8859-1"), 0, str.length());
1) Will the exceptions ever be thrown? How can possibly MD5 algotithm or “iso-8859-1” encoding be missing?
2) I’m wondering what can I do if I happen to catch one of these exceptions; the answer is: probably nothing! So wouldn’t make more sense to wrap the exception into a new Runtime exception (probably something like MD5HashingException)?
The typical pattern here is:
That’s how you express: “In my situation, I am confident that this exception isn’t going to happen”. If you’re sure that the code will never be run in some exotic boot class path that lacks MD5 or ISO-8859-1, this is the solution.