您的位置:首页 > 其它

计算字符串MD5值简单实现

2012-01-04 11:12 197 查看
public class Md5Util {

/**
* 取得字符串的MD5值
*
* @param str
* @return
*/
public static String getMd5(String str) {

char[] charArray = str.toCharArray();

byte[] byteArray = new byte[charArray.length];

for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];

byte[] md5Bytes = null;

try {
md5Bytes = MessageDigest.getInstance("MD5").digest(byteArray);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}

StringBuffer hexValue = new StringBuffer();

for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}

return hexValue.toString();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: