Skip to content

Instantly share code, notes, and snippets.

@beaucollins
Created September 17, 2014 20:55
Show Gist options
  • Save beaucollins/44a4aaa592a5b6b1c2a5 to your computer and use it in GitHub Desktop.
Save beaucollins/44a4aaa592a5b6b1c2a5 to your computer and use it in GitHub Desktop.
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Md5Util {
public static String hex(byte[] array) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; ++i) {
sb.append(Integer.toHexString((array[i]
& 0xFF) | 0x100).substring(1,3));
}
return sb.toString();
}
public static String convertToMd5(String message) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
return hex(md.digest(message.getBytes("CP1252")));
} catch (NoSuchAlgorithmException e) {
// TODO: handle error
} catch (UnsupportedEncodingException e) {
// TODO: handle error
}
return null;
}
}
String md5hash = Md5Util.convertToMd5("[email protected]");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment