MD5 in JavaThis is how you emulate the m5sum linux command or md5() php funtion in Java:
public class MD5 { private MD5() { //harrrr ;) } /** * Calculates md5 hash of a string. The string first gets * transformed into UTF-8 and then hashed by the java.security md5 algorithm * @param token the string which will be hashed * @return the md5 hash as hex string */ public static String calcMD5(String token) { MessageDigest md; if(token == null) return null; try { md = MessageDigest.getInstance("MD5"); } catch(Exception e) { System.err.println(e); return null; } md.reset(); try { md.update(token.getBytes("UTF-8")); } catch(Exception e) { System.err.println(e); } byte digest = md.digest(); StringBuffer out = new StringBuffer(32); String add; for(int i=0; i<digest.length; i++) { add = Integer.toHexString( ((int)digest[i]) & 0xff ); if (add.length() < 2) out.append('0'); out.append(add); } return out.toString(); } }
|