public class MD5 {函數
public static String getMD5(String str) {加密
try {spa
// 生成一個MD5加密計算摘要orm
MessageDigest md = MessageDigest.getInstance("MD5");md5
// 計算md5函數字符串
md.update(str.getBytes());get
// digest()最後肯定返回md5 hash值,返回值爲8爲字符串。由於md5 hash值是16位的hex值,實際上就是8位的字符hash
// BigInteger函數則將8位的字符串轉換成16位hex值,用字符串來表示;獲得字符串形式的hash值it
return new BigInteger(1, md.digest()).toString(16);io
} catch (Exception e) {
throw new RuntimeException("MD5加密出現錯誤");
}
}
}