作者:cndz 围观群众:653 更新于 标签:md5工具类Java工具类
package demo.utils;
import lombok.extern.slf4j.Slf4j;
import java.security.MessageDigest;
/**
* MD5加密工具类
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://docs.zhyd.me
* @date 2018/4/18 11:48
* @since 1.0
*/
@Slf4j
public class Md5Util {
/**
* 通过盐值对字符串进行MD5加密
*
* @param param
* 需要加密的字符串
* @param salt
* 盐值
* @return
*/
public static String MD5(String param, String salt) {
return MD5(param + salt);
}
/**
* 加密字符串
*
* @param s
* 字符串
* @return
*/
public static String MD5(String s) {
char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
try {
byte[] btInput = s.getBytes();
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(btInput);
byte[] md = mdInst.digest();
int j = md.length;
char[] str = new char[j * 2];
int k = 0;
for (byte byte0 : md) {
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
log.error("MD5生成失败", e);
return null;
}
}
public static String MD5_16(String s){
return MD5(s).substring(8,24);
}
public static String MD5_16(String str1,String str2){
return MD5_16(str1 + str2);
}
public static void main(String[] args) {
System.out.println(MD5("123"));
System.out.println(MD5_16("123"));
}
}