How to generate HMAC-SHA1 Signature in Android?
返回String为加密后字符串数据
/**
* 获取 hmacSha1
*
* @param base
* @param key
* @return
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
*/
public static String hmacSha1(String base, String key) throws NoSuchAlgorithmException, InvalidKeyException {
if (TextUtils.isEmpty(base) || TextUtils.isEmpty(key)) {
return "";
}
String type = "test-HmacSHA1";
SecretKeySpec secret = new SecretKeySpec(key.getBytes(), type);
Mac mac = Mac.getInstance(type);
mac.init(secret);
byte[] digest = mac.doFinal(base.getBytes());
return Base64.encodeToString(digest, Base64.DEFAULT);
}
本文详细介绍了如何在Android应用中使用HMAC-SHA1算法生成签名,并将加密后的字符串数据转换为Base64编码。包括密钥和基础字符串的参数验证,确保生成过程的正确性和安全性。
247

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



