public class HMACSHA256 {
public static String hmacSha256(String KEY, String VALUE) {
return hmacSha(KEY, VALUE, "HmacSHA256");
}
private static String hmacSha(String KEY, String VALUE, String SHA_TYPE) {
try {
SecretKeySpec signingKey = new SecretKeySpec(KEY.getBytes("UTF-8"), SHA_TYPE);
Mac mac = Mac.getInstance(SHA_TYPE);
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(VALUE.getBytes("UTF-8"));
byte[] hexArray = {
(byte)'0', (byte)'1', (byte)'2', (byte)'3',
(byte)'4', (byte)'5', (byte)'6', (byte)'7',
(byte)'8', (byte)'9', (byte)'a', (byte)'b',
(byte)'c', (byte)'d', (byte)'e', (byte)'f'
};
byte[] hexChars = new byte[rawHmac.length * 2];
for ( int j = 0; j < rawHmac.length; j++ ) {
int v = rawHmac[j] & 0xFF;
android hmacSha256 加密
最新推荐文章于 2025-04-25 18:00:00 发布
这段代码展示了如何使用HMAC-SHA256算法对字符串进行安全的哈希编码。它首先创建一个SecretKeySpec实例,然后通过Mac类初始化并执行哈希操作。最终将原始哈希转换为16进制字符串返回。

最低0.47元/天 解锁文章
1402

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



