原 ShaUtils 工具类
版权声明:本文为博主原创文章,请尊重他人的劳动成果,转载请附上原文出处链接和本声明。
本文链接:https://www.91mszl.com/zhangwuji/article/details/1051
package com.mszl.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.security.MessageDigest;
/**
* SHA256加密工具类
*/
public class ShaUtils {
private static final Logger logger = LoggerFactory.getLogger(ShaUtils.class);
public static String encodeSHA256(String testString) {
byte[] data = testString.getBytes();
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] digest = md.digest(data);
return bytes2Hex(digest);
} catch (Exception e) {
logger.warn("加密失败", e);
return "";
}
}
private static String bytes2Hex(byte[] bts) {
StringBuilder sb = new StringBuilder();
String tmp;
for (byte bt : bts) {
tmp = (Integer.toHexString(bt & 0xFF));
if (tmp.length() == 1) {
sb.append("0");
}
sb.append(tmp);
}
return sb.toString().toUpperCase();
}
}
2019-08-23 14:22:32 阅读(1638)
名师出品,必属精品 https://www.91mszl.com
博主信息