Hello I'm trying to create Http Request to Api which needs data SHA512 encrypted. I've made same example in C# that works. In Android Java I can not reproduce hash and authenticate to WebApi. I think that the problem is that
mac.doFinal(byteData);
is creating byte array with negative values. In C# there are none negatives. Here is my code. Please tell me what am I doing wrong:
public static String calculateHMAC(String secret, String data) {
byte[] byteSecret = secret.getBytes(StandardCharsets.UTF_8);
byte[] byteData = data.getBytes(StandardCharsets.UTF_8);
try {
SecretKeySpec signingKey = new SecretKeySpec(byteSecret, "HmacSHA512");
Mac mac = Mac.getInstance("HmacSHA512");
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(byteData); // -> Here Java makes rawMac with negative bytes
return byteArrayToString(rawHmac);
} catch (GeneralSecurityException e) {
throw new IllegalArgumentException();
}
}
private static String byteArrayToString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for(byte b : bytes){
sb.append(Integer.toHexString(0xff & b));
}
return sb.toString();
}
Thanks in advance