Created
September 17, 2014 20:55
-
-
Save beaucollins/44a4aaa592a5b6b1c2a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String md5hash = Md5Util.convertToMd5("[email protected]"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment