package com.bjsxt.util;
import java.security.MessageDigest;
public class Md5Util {
/**
* 接收一個明文密碼, 返回加密以後的密文
*
* @param pwd
* @return
*/
public static String md5(String pwd) throws Exception {
// 創建MessageDigest對象
MessageDigest digest = MessageDigest.getInstance("MD5");
// 對明文進行加密
byte[] temp = digest.digest(pwd.getBytes());
// 準備StringBuilder用於保存結果
StringBuilder builder = new StringBuilder();
// 遍歷字節數組, 一個字節轉換爲長度爲2的字符串
for (byte b : temp) {
// 去除負數
String s = Integer.toHexString(b & 0xff);
// 補零
if(s.length() == 1) {
builder.append(0);
}
builder.append(s);
}
return builder.toString();
}
}