Skip to content

Instantly share code, notes, and snippets.

@deepankarb
Created September 17, 2017 04:47
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save deepankarb/b474b89a1081bd3be9533ace61c9c9a5 to your computer and use it in GitHub Desktop.
java MD5
public static String md5(String in) {
byte[] bytes;
StringBuilder hashtext;
try {
bytes = in.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] digest = md.digest(bytes);
BigInteger bigInt = new BigInteger(1, digest);
hashtext = new StringBuilder(bigInt.toString(16));
while (hashtext.length() < 32) {
hashtext.insert(0, "0", 0, 1);
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e.getMessage());
}
return hashtext.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment