最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

SHA

IT圈 admin 6浏览 0评论

SHA

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;}
}

SHA

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;}
}

发布评论

评论列表 (0)

  1. 暂无评论