September 14, 2006

MD5 Hash

// Here is a simple example on how to encrypt a string using MD5.

import java.security.MessageDigest;


public static String md5(String data) throws Exception {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.reset();
md5.update(data.getBytes());
byte messageDigest[] = md5.digest();

StringBuffer hexString = new StringBuffer();
for (int i=0;i < messageDigest.length;i++) {
hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
}

return hexString.toString();
}

No comments: