導入Java . security . nosuchalgorithm exception;
公共類MD5 {
私有MessageDigest md
私有靜態MD5 md5
私有MD5()
{
嘗試{
MD = message digest . getinstance(" MD5 ");
} catch(nosuch algorithm exception e){
System.out.println("無此算法");
}
}
//生成MD5實例。
公共靜態MD5 getInstance()
{
if(null!=md5)
返回md5
其他
{
make instance();
返回md5
}
}
//確保只有壹個線程同時使用MD5加密。
私有靜態同步void makeInstance()
{
if(null==md5)
MD5 = new MD5();
}
公共字符串createMD5(字符串傳遞)
{
MD . update(pass . getbytes());
byte[]b = MD . digest();
返回byteToHexString(b);
}
私有字符串byteToHexString(byte[] b)
{
string buffer sb = new string buffer();
String temp =
for(int I = 0;我& ltb .長度;i++)
{
temp = integer . tohexstring(b[I]& amp;0x ff);
if(temp.length()==1)
temp = " 0 "+temp;
sb.append(臨時);
}
return sb . tostring();
}
公共靜態void main(String[] args) {
system . out . println(MD5 . getinstance()create MD5(" 123 "));}}