I can’t get Ruby and Java to agree on a HMAC:
Ruby:
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
Java:
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
Can you spot what’s wrong?
Edit: I fixed the string that was badly copy/pasted.
Edit2: I reverted the fix, and use the proper content on ruby string so the answers and the accepted answer still make sense. Sorry for the mess
The messages are different – see the
dfoA9\nin the Ruby block.