package cn.com.easytaxi.util;
public class MD5
{
public static String getMD5(byte[] source)
{
String s = null;
char hexDigits[] = { // 用来将字节转换成 16 进制表示的字符
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
try
{
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
md.update(source);
byte tmp[] = md.digest(); // MD5 的计算结果是一个 128 位的长整数,
// 用字节表示就是 16 个字节
char str[] = new char[16 * 2]; // 每个字节用 16 进制表示的话,使用两个字符,
// 所以表示成 16 进制需要 32 个字符
int k = 0; // 表示转换结果中对应的字符位置
for (int i = 0; i < 16; i++)
{ // 从第一个字节开始,对 MD5 的每一个字节
// 转换成 16 进制字符的转换
byte byte0 = tmp[i]; // 取第 i 个字节
str[k++] = hexDigits[byte0 >>> 4 & 0xf]; // 取字节中高 4 位的数字转换,
// >>> 为逻辑右移,将符号位一起右移
str[k++] = hexDigits[byte0 & 0xf]; // 取字节中低 4 位的数字转换
}
s = new String(str); // 换后的结果转换为字符串
}
catch (Exception e)
{
e.printStackTrace();
}
return s;
}
public static void main(String[] args)
{
System.out.println(getMD5("itolrgvh".getBytes()));
}
}
最近下载更多
最近浏览更多
微信网友_6906962132258816 LV7
2024年5月15日
许文欣 LV2
2022年3月26日
sodkj123 LV16
2021年10月26日
别碰我被窝 LV9
2021年10月25日
CoderMars LV13
2021年8月23日
Myangyyyy LV10
2021年3月5日
13043860zj LV16
2021年2月24日
JIeless LV2
2021年2月5日
2252536772 LV21
2021年2月4日
sssssssyy LV10
2020年12月17日

