package com.ctrl.mobile.respUtils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @Author LiPeiMin
* @Description //TODO MD5加密
* @Date 11:38 2019/11/18
**/
public class MD5 {
private static MessageDigest mdigest = null;
//String类的toUpperCase()和toLowerCase()方法 这里加密后的是小写的
private static char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
private static MessageDigest getMdInst() {
if (null == mdigest) {
try {
mdigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
return mdigest;
}
/**
* @Description TODO //MD5加密
* @Param [s]
* @Return java.lang.String
* @Author wuyu
* @Date 13:49 2019/11/29
*/
public static String encode(String s) {
if (null == s) {
return "";
}
try {
byte[] bytes = s.getBytes();
getMdInst().update(bytes);
byte[] md = getMdInst().digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = digits[byte0 >>> 4 & 0xf];
str[k++] = digits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
来源:oschina
链接:https://my.oschina.net/u/4149060/blog/3135456