我试图找出为什么openssl中的hmac没有给我与java中的hmac相同的结果.
在开放的ssl
echo -n "Hello" | openssl dgst -sha256 -hmac 04d6b077d60e323711b37813b3a68a71
输出:cc598d8840fe409d5fcc1c1c856f9e8c311d1c458850615555857b023f1cd94c
在java中
String key = "04d6b077d60e323711b37813b3a68a71" SecretKeySpec key2 = new SecretKeySpec(Hex.decode(key), "RAW"); String data = "Hello"; Mac hmac = Mac.getInstance("Hmac-SHA256", BouncyCastleProvider.PROVIDER_NAME); hmac.init(key2) byte[] bytes = hmac.doFinal(data.getBytes()); System.out.println(Hex.toHexString(bytes));
输出:877f9c8eb44c20987e3978928fbfcea0f1cf99c88f9db904596921b7ecf0613b
我不知道为什么这些不同.