直接代碼:
public static String signWithHmacSHA256(String message, String secret) {
if (StringUtils.isBlank(secret)) {
return null;
}
try {
Mac HmacSHA256 = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(CharsetUtil.UTF_8), "HmacSHA256");
HmacSHA256.init(secret_key);
message = URLEncoder.encode(message, "UTF-8");
byte[] bytes = HmacSHA256.doFinal(message.getBytes(CharsetUtil.UTF_8));
return Base64.encode(bytes);
} catch (Exception e) {
return null;
}
}