StringUtil 工具类包含如下:
1.判断字符串是否为空
2.生成MD5加密计算
3.base64编码
4.base64解码
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Base64;
/**
* @author: xiaolh
* @email: amixiao@qq.com
* @createTime: 2021/3/20 14:48
*/
public class StringUtil {
/**
* 判断字符串是否为空
* @param str
* @return
*/
public static boolean isEmpty(String str) {
return str == null || str.trim().length() == 0;
}
public static String encryptMD5(String str) {
try {
// 生成一个MD5加密计算摘要
MessageDigest md = MessageDigest.getInstance("MD5");
// 计算md5函数
md.update(str.getBytes());
// digest()最后确定返回md5 hash值,返回值为8位字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
// BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值
//一个byte是八位二进制,也就是2位十六进制字符(2的8次方等于16的2次方)
return new BigInteger(1, md.digest()).toString(16);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* base64编码
* @param str
* @return
*/
public static String encryptBASE64(String str) {
return new String(Base64.getEncoder().encode(str.getBytes()));
}
/**
* base64解码
* @param str
* @return
*/
public static String decodeBASE64(String str) {
return new String(Base64.getDecoder().decode(str));
}
}