import java.security.*;
public class MD5
{
private static MD5 md5 = null;
private MD5(){}
public static MD5 getInstance()
{
if (md5 == null)
{
md5 = new MD5();
}
return md5;
}
/**
* Description:encrypt the String by "MD5"
* @param str String
* @return String
*/
public String calcMD5(String str)
{
try
{
MessageDigest alga = MessageDigest.getInstance("MD5");
alga.update(str.getBytes());
byte[] digesta = alga.digest();
return byte2hex(digesta);
}
catch (Exception e)
{
//System.out.println(e.getMessage());
return null;
}
}
private String byte2hex(byte[] b)
{
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++)
{
stmp = (Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
{
hs = hs + "0" + stmp;
}
else
{
hs = hs + stmp;
}
if (n < b.length - 1)
{
hs = hs + "";
}
}
return hs;
}
}
posted on 2006-12-08 13:49
robbin163 阅读(349)
评论(1) 编辑 收藏