Skip to content

Instantly share code, notes, and snippets.

@yukoba
Created November 4, 2012 12:12
Show Gist options
  • Save yukoba/4011666 to your computer and use it in GitHub Desktop.
Save yukoba/4011666 to your computer and use it in GitHub Desktop.
MD5 Java
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);
}
}
}
@AiziChen
Copy link

bug: need to replace with hexString.append(String.format("%02x", b)); on the 15 line of code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment