Java HMAC_SHA256实现

HMAC_SHA256在PHP中可以通过系统函数hash_hmac实现

$hash = hash_hmac('sha256', "abcdefg", "123");

java中就要麻烦一点


import org.apache.commons.codec.binary.Hex;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class EncryptUtil { 
    /**
     * @param plainStr 需要加密的字符串
     * @param key
     * @return 加密后的字符串
     */
    public static String hmacsha256(String plainStr, String key) {
        SecretKeySpec secretKey = new
                SecretKeySpec(key.getBytes(Charset.forName("UTF-8")),
                "HmacSHA256");
        Mac mac = null;
        try {
            mac = Mac.getInstance(secretKey.getAlgorithm());
            mac.init(secretKey);
        } catch (NoSuchAlgorithmException | InvalidKeyException e) {
            e.printStackTrace();
        }

        byte digest[] = mac.doFinal(plainStr.getBytes(Charset.forName("UTF-8")));
        return new StringBuilder().append(byte2HexStr(digest)).toString();
    }

    public static String byte2HexStr(byte array[]) {
        return array != null ? new String(Hex.encodeHex(array)) : null;
    }
    public static void main(String[] args) {
        String hash = EncryptUtil.hmacsha256("abcdefg", "123");
        System.out.println(hash);
    }
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个 Java 实现的示例,可以用于在 HTTP 请求头部中使用 HMAC-SHA256 加密: ```java import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Base64; public class HmacSha256Example { public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException { String secretKey = "your_secret_key"; // 你的密钥 String message = "your_message_to_sign"; // 要签名的消息 String signature = generateHmacSha256Signature(secretKey, message); System.out.println("HMAC-SHA256 signature: " + signature); } private static String generateHmacSha256Signature(String secretKey, String message) throws NoSuchAlgorithmException, InvalidKeyException { String algorithm = "HmacSHA256"; Mac mac = Mac.getInstance(algorithm); SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), algorithm); mac.init(signingKey); byte[] signatureBytes = mac.doFinal(message.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(signatureBytes); } } ``` 在上述示例中,我们首先定义了一个 `secretKey` 和一个 `message`。然后通过 `generateHmacSha256Signature` 方法,使用 `Mac` 类来实现 HMAC-SHA256 加密。最终,我们通过 Base64 编码将签名结果转换成字符串并返回。 注意,这里的 `secretKey` 和 `message` 都是示例数据,你需要将其替换成你自己的数据。另外,这里使用的是 Java 标准库中的 `javax.crypto` 包,你需要确保你的代码中已经引入了这个包。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值