Skip to content

Instantly share code, notes, and snippets.

@peakpg
Created April 19, 2011 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peakpg/929652 to your computer and use it in GitHub Desktop.
Save peakpg/929652 to your computer and use it in GitHub Desktop.
private String encodeToMD5(String message) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException{
byte[] bytesOfMessage = message.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] messageDigest = md.digest(bytesOfMessage);
BigInteger number = new BigInteger(1, messageDigest);
String hashtext = number.toString(16);
// Now we need to zero pad it if you actually want the full 32 chars.
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
return hashtext;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment