/**
*
* @param s 需加密的字符串
* @param key 秘钥
* @return 加密
* @throws Exception
*/
public static String sign(String s, String key)
throws Exception {
//HMAC-SHA1 算法签名
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"),
mac.getAlgorithm());
mac.init(secretKeySpec);
byte[] hash = mac.doFinal(s.getBytes("UTF-8"));
String result = "";
String tmp = "";
for (byte bt : hash) {
tmp = (Integer.toHexString(bt & 0xFF));
if (tmp.length() == 1) {
result += "0";
}
result += tmp;
}
return result;
}
JAVA 通过HmacSHA1+密钥加密
最新推荐文章于 2026-06-28 11:37:36 发布
本文介绍了一种使用HMAC-SHA1算法进行字符串签名的方法。通过具体实现步骤,展示了如何利用Java编程语言中的Mac类和SecretKeySpec类完成加密过程。此方法适用于需要对数据进行安全验证的应用场景。
3774

被折叠的 条评论
为什么被折叠?



