谁能告诉我一个简洁可用的方法用java求一个字符串生成的md5码。
展开全部
public class MD5 {
public static String crypt(String str) throws NoSuchAlgorithmException {
if (str == null || str.length() == 0) {
throw new IllegalArgumentException();
}
StringBuffer hexString = new StringBuffer();
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());
byte[] hash = md.digest();
for (int i = 0; i < hash.length; i++) {
if ((0xff & hash[i]) < 0x10) {
hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
} else {
hexString.append(Integer.toHexString(0xFF & hash[i]));
}
}
return hexString.toString();
}
public static String crypt(String str) throws NoSuchAlgorithmException {
if (str == null || str.length() == 0) {
throw new IllegalArgumentException();
}
StringBuffer hexString = new StringBuffer();
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());
byte[] hash = md.digest();
for (int i = 0; i < hash.length; i++) {
if ((0xff & hash[i]) < 0x10) {
hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
} else {
hexString.append(Integer.toHexString(0xFF & hash[i]));
}
}
return hexString.toString();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询