如題
java
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Md5
{
private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
/**
* md5編碼
*
* @param str
* @return
* @throws NoSuchAlgorithmException
*/
public static String encode(String str) throws NoSuchAlgorithmException
{
// 生成一個MD5加密計算摘要
MessageDigest md5 = MessageDigest.getInstance("MD5");
// 使用指定的字節(jié)數(shù)組對摘要進行最后更新,然后完成摘要計算
byte[] results = md5.digest(str.getBytes());
//將得到的字節(jié)數(shù)組變成字符串返回
String result = byteArrayToHexString(results);
return result;
}
private static String byteArrayToHexString(byte[] b)
{
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}
private static String byteToHexString(byte b)
{
int n = b;
if (n < 0) {
n = 256 + n;
}
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
}
// Hello World!
// ed076287532e86365e841e92bfc50d8c
php
md5('Hello World!');
// Hello World!
// ed076287532e86365e841e92bfc50d8c```
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。