Java: generate MD5 digest in Hex

importjava.security.MessageDigest;importjava.security.NoSuchAlgorithmException;publicclassMD5Hex{publicstaticStringMD5Hex(Strings){Stringresult=null;try{MessageDigestmd5=MessageDigest.getInstance("MD5");byte[]digest=md5.digest(s.getBytes());result=toHex(digest);}catch(NoSuchAlgorithmExceptione){// this won't happen, we know Java has MD5!}returnresult;}publicstaticStringtoHex(byte[]a){StringBuildersb=newStringBuilder(a.length*2);for(inti=0;i<a.length;i++){sb.append(Character.forDigit((a[i]&0xf0)>>4,16));sb.append(Character.forDigit(a[i]&0x0f,16));}returnsb.toString();}publicstaticvoidmain(String[]args){System.out.println("MD5 for abcde:\t"+MD5Hex("abcde"));System.out.println("MD5 for bbb:\t"+MD5Hex("bbb"));System.out.println("MD5 for abcde:\t"+MD5Hex("abcde"));System.out.println("MD5 for 12345:\t"+MD5Hex("12345"));}}