21,887
社区成员
发帖
与我相关
我的任务
分享
public final class MD5
{
public static String digest(String txt) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(txt.getBytes("UTF-8"));
return toHex(md.digest());
}
catch (NoSuchAlgorithmException e) {
return "";
} catch (UnsupportedEncodingException e) {
return "";
}
}
public static String digest(String txt, boolean toLowerCase) {
if (toLowerCase) {
return digest(txt).toLowerCase();
}
return digest(txt);
}
private static final char[] hexDigits = {
'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
private static String toHex(byte[] byteArray) {
char[] resultCharArray = new char[byteArray.length * 2];
int index = 0; byte b1; int i; byte[] arrayOfByte;
for (i = arrayOfByte = byteArray.length, b1 = 0; b1 < i; ) { byte b = arrayOfByte[b1];
resultCharArray[index++] = hexDigits[b >>> 4 & 0xF];
resultCharArray[index++] = hexDigits[b & 0xF]; b1++; }
return new String(resultCharArray);
}
}