public class Md5Utils {
/**
* 对给定字符串 origin 进行md5加密
*
* md5 : Message Digest 5 Algorithm 消息摘要算法
* 是一种符合工业标准的"单向"128位的Hash算法
*
* @param origin 原字符串
* @return 加密后的字符串
*/
public static String getMd5(String origin){
try {
// 主要使用类 -> MessageDigest
// 这里是选择加密方式,我们用MD5
MessageDigest md = MessageDigest.getInstance("MD5");
// 将字符串使用utf-8编码,得到字节数组
byte[] input = origin.getBytes("utf-8");
// 3) 使用digest(input)对字节数组进行md5的哈希计算,得到加密以后的字节数组,长度是16个字节。
byte[] digest = md.digest(input);
// 4) 使用类BigInteger(1, 加密后的字节数组),将这个二进制数组转成无符号的大整数
BigInteger bigInteger = new BigInteger(1, digest);
// 转换成16进制字符串返回
return bigInteger.toString(16);
} catch (Exception e) {
e.printStackTrace();
// 做终止用
throw new RuntimeException(e);
}
}
}