Md5加密简化代码

 

public static String getMd5(String mess){

StringBuilder ss = new StringBuilder();

try {

MessageDigest md = MessageDigest.getInstance("MD5");

byte[] digest = md.digest(mess.getBytes());

for (byte b : digest){

//把byte 转成16进制数

//Integer.toHexString(hashCode())

int i = b;// 1byte  4byte 

//清空掉前三个字节 变为0

i = 0x000000ff &i;

String hex = Integer.toHexString(i);

if (hex.length() == 1) {

hex = "0" + hex;

}

//拼接

ss.append(hex);

}

} catch (NoSuchAlgorithmException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return ss + "";

}

 

posted @ 2016-07-19 22:38  Amy365  阅读(355)  评论(0)    收藏  举报