Created
November 4, 2012 12:12
-
-
Save yukoba/4011666 to your computer and use it in GitHub Desktop.
MD5 Java
This file contains 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.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
public class MD5Utils { | |
public static String convert(String s) { | |
try { | |
// Create MD5 Hash | |
MessageDigest digest = MessageDigest.getInstance("MD5"); | |
digest.update(s.getBytes()); | |
byte messageDigest[] = digest.digest(); | |
// Create Hex String | |
StringBuilder hexString = new StringBuilder(); | |
for (byte b : messageDigest) { | |
hexString.append(Integer.toHexString(0xFF & b)); | |
} | |
return hexString.toString(); | |
} catch (NoSuchAlgorithmException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bug: need to replace with
hexString.append(String.format("%02x", b));
on the 15 line of code.