HmacSHA256签名加Base64编码加URL编码

/**
*先使用HmacSHA256签名,再使用Base64编码,最后进行URL 编码
*signatureReqStr : 待加密data
* secretKey : 密钥
*/
public static String getSignature(String signatureReqStr,String secretKey){
Mac sha256_HMAC ;
String result = "";
try {
sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
result = Base64.encodeBase64String(sha256_HMAC.doFinal(signatureReqStr.getBytes()));
result = URLEncoder.encode(result);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return result;
}
听说学习能够让青春永驻。
原文地址:https://www.cnblogs.com/chenyf/p/8494542.html