import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5HashUtil
{
private MessageDigest md = null;
private static MD5HashUtil md5 = null;
private static final char[] hexChars ={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
/**
* Constructor is private so you must use the getInstance method
*/
private MD5HashUtil() throws NoSuchAlgorithmException
{
md = MessageDigest.getInstance("MD5");
}
/**
* This returns the singleton instance
*/
public static MD5HashUtil getInstance()throws NoSuchAlgorithmException
{
if (md5 == null)
{
md5 = new MD5HashUtil();
}
return (md5);
}
public static String hashCode(String dataToHash) throws NoSuchAlgorithmException{
return getInstance().hashData(dataToHash.getBytes());
}
public static String hashCode(byte[] dataToHash) throws NoSuchAlgorithmException{
return getInstance().hashData(dataToHash);
}
public String hashData(byte[] dataToHash) {
return hexStringFromBytes((calculateHash(dataToHash))).toLowerCase();
}
private byte[] calculateHash(byte[] dataToHash)
{
md.update(dataToHash, 0, dataToHash.length);
return (md.digest());
}
public String hexStringFromBytes(byte[] b)
{
String hex = "";
int msb;
int lsb = 0;
int i;
// MSB maps to idx 0
for (i = 0; i < b.length; i++)
{
msb = ((int)b[i] & 0x000000FF) / 16;
lsb = ((int)b[i] & 0x000000FF) % 16;
hex = hex + hexChars[msb] + hexChars[lsb];
}
return(hex);
}
public static void main(String args[]) throws NoSuchAlgorithmException
{
String string = "my name is zhangli";
System.out.println(string);
System.out.println(hashCode(string));
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有