Created
April 27, 2013 10:55
-
-
Save mingcheng/5472680 to your computer and use it in GitHub Desktop.
Android/Java equivalent of MD5 function in PHP
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
| public static String md5(String string) { | |
| byte[] hash; | |
| try { | |
| hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8")); | |
| } catch (NoSuchAlgorithmException e) { | |
| throw new RuntimeException("Huh, MD5 should be supported?", e); | |
| } catch (UnsupportedEncodingException e) { | |
| throw new RuntimeException("Huh, UTF-8 should be supported?", e); | |
| } | |
| StringBuilder hex = new StringBuilder(hash.length * 2); | |
| for (byte b : hash) { | |
| int i = (b & 0xFF); | |
| if (i < 0x10) hex.append('0'); | |
| hex.append(Integer.toHexString(i)); | |
| } | |
| return hex.toString(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment