2

我无法让 Ruby 和 Java 就 HMAC 达成一致:

红宝石:

hmac_key = "my hmac key" #this is THE REAL KEY
msg = "fCyVmpFAZxv9Utui2QWGUtoGJ//Zr5aH+1PV31ry/dwX3yVdeEMIMW/dfoA9\nihbnYrnoSnb2yyfOrBYoy0JlDvWz8GJ6dY643lDTj7xcw8Q="
hashb16  = OpenSSL::HMAC.hexdigest('sha256', hmac_key, msg)
puts hashb16

052310d1fc91df4e5bbb47194cc401feed51eca98668b501555c78774dad6bed

爪哇:

String hmac_key = "my hmac key";
String encrypted_message_b64 = "fCyVmpFAZxv9Utui2QWGUtoGJ//Zr5aH+1PV31ry/dwX3yVdeEMIMW/dfoA9ihbnYrnoSnb2yyfO\nrBYoy0JlDvWz8GJ6dY643lDTj7xcw8Q=";
final Mac hmac = Mac.getInstance("HmacSHA256");
hmac.init(new SecretKeySpec(hmac_key.getBytes("UTF-8"), "HmacSHA256"));
byte[] signature = hmac.doFinal(encrypted_message_b64.getBytes("UTF-8"));
System.out.println(Hex.encodeHexString(signature));

d5bc0b58f43c6f6611f63822d22f99e18c51a33251a5a1c0c7712b4c7fb1ad24

你能看出哪里不对吗?


编辑:我修复了严重复制/粘贴的字符串。

Edit2:我恢复了修复,并在 ruby​​ 字符串上使用了正确的内容,所以答案和接受的答案仍然有意义。对不起混乱

4

2 回答 2

5

消息不同 - 请参阅dfoA9\nRuby 块中的 。

于 2012-10-15T12:37:15.303 回答
2

我不知道您通过复制和粘贴“修复”了什么,两个源字符串仍然不同(\n在不同的位置),并且您发布的哈希现在相等。我想你没有他们平等,否则你不会发布问题。

But what i see is that you are using b64_encoded as the Ruby input, which is not the msg you are showing us. Is there a chance it has a different content?

于 2012-10-15T12:56:25.613 回答