Skip to content

Instantly share code, notes, and snippets.

@tehcpu
Created July 13, 2015 15:17
Show Gist options
  • Save tehcpu/07ec892d2dd96ee64b8f to your computer and use it in GitHub Desktop.
Save tehcpu/07ec892d2dd96ee64b8f to your computer and use it in GitHub Desktop.
public static String md5Custom(String st) {
MessageDigest messageDigest = null;
byte[] digest = new byte[0];
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(st.getBytes());
digest = messageDigest.digest();
} catch (NoSuchAlgorithmException e) {
// тут можно обработать ошибку
// возникает она если в передаваемый алгоритм в getInstance(,,,) не существует
e.printStackTrace();
}
BigInteger bigInt = new BigInteger(1, digest);
String md5Hex = bigInt.toString(16);
while( md5Hex.length() < 32 ){
md5Hex = "0" + md5Hex;
}
return md5Hex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment