public static byte[] hmacSha1Encrypt(String encryptText, String encryptKey) {
try {
byte[] text = encryptText.getBytes("UTF8");
byte[] keyData = encryptKey.getBytes("UTF8");
SecretKeySpec secretKey = new SecretKeySpec(keyData, "HmacSHA1");
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
return mac.doFinal(text);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
加密后,直接返回的是 byte[] 结构,之后如果需要,再转成 string,或者进行base64编码成字符串。