代码简单 直接上
/**
* MD5工具类
*/
package com.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.xml.bind.DatatypeConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
*/
public class MD5Util {
private static Logger logger = LoggerFactory.getLogger(MD5Util.class);
private static final String encryModel = "MD5";
/**
* <p>Title: </p>
* <p>Description: </p>
*/
private MD5Util() {
super();
}
//可选MD5 origin:待加密数据
public static String md5(String origin) {
return encrypt(origin, "md5");
}
//可选sha-1 origin:待加密数据
public static String sha(String origin) {
return encrypt(origin, "sha-1");
}
/**
* @param origin
* 加密内容
* @param algorithmName
* md5或者sha-1
*/
public static String encrypt(String origin,String algorithmName) {
String localAlgorithmName ;
if (algorithmName == null || "".equals(algorithmName.trim())) {
localAlgorithmName = "md5";
}else{
localAlgorithmName = algorithmName;
}
try {
MessageDigest md5 = MessageDigest.getInstance(localAlgorithmName);
md5.update(origin.getBytes());
return DatatypeConverter.printHexBinary(md5.digest()).toLowerCase();
} catch (NoSuchAlgorithmException e) {
logger.error("MD5加密失败"+e);
}
return null;
}
}
网友评论