MD5 加密(MessageDigest)
abstract class MessageDigest extends MessageDigestSpi
/**
* md5 明文字符加密
*
* @param source
* @return
*/
public static String md5Encrypt(String source) {
// 1.判断source是否有效
if (StringUtils.isBlank(source)) {
throw new RuntimeException(CrowdConstant.MESSAGE_STRING_INVALIDATE);
}
String algorithm = "md5";
try {
// 2.获取MessageDigest 加密对象
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
// 3.获取明文字符串对应的字节数组
byte[] input = source.getBytes();
// 4.执行加密
byte[] output = messageDigest.digest(input);
// 5.转字节数组
int signum = 1;
BigInteger bigInteger = new BigInteger(signum, output);
// 6.按照16进制将biginteger的值转换为字符串
int radix = 16;
String encoded = bigInteger.toString(16);
return encoded;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}