SHA-256、HMAC-SHA256加密算法工具类
SHA-256、HMAC-SHA256加密算法工具类
·
public class CryptoUtils {
private static final String SHA_256 = "SHA-256";
private static final String HMAC_SHA_256 = "HmacSHA256";
private CryptoUtils() {
}
/**
* SHA-256 加密算法
*/
public static String sha256(String message) {
String res;
try {
MessageDigest messageDigest = MessageDigest.getInstance(SHA_256);
byte[] hash = messageDigest.digest(message.getBytes(StandardCharsets.UTF_8));
res = Hex.encodeHexString(hash);
} catch (NoSuchAlgorithmException e) {
return null;
}
return res;
}
/**
* HMAC-SHA256 签名算法
*/
public static String hmacSha256(String secret, String message) {
String res;
try {
Mac mac = Mac.getInstance(HMAC_SHA_256);
SecretKey secretKey = new SecretKeySpec(secret.getBytes(), HMAC_SHA_256);
mac.init(secretKey);
byte[] hash = mac.doFinal(message.getBytes());
res = Hex.encodeHexString(hash);
} catch (Exception e) {
return null;
}
return res;
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)