/**
*
* @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+密钥加密
最新推荐文章于 2024-08-11 23:01:51 发布