Reputation: 23
I need to sign a message with a MAC using SHA-256 algorithm. I have the code for generating the MAC, however validation on the other end is failing. I've been told that the value I am calculating is wrong.
Here is my code
public static byte[] calculateMAC(byte[] _aiOutBufferForMacCalculation, String key) {
try{
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] mac_data=sha256_HMAC.doFinal(_aiOutBufferForMacCalculation);
String result = "";
for (final byte element : mac_data)
{
result += Integer.toString((element & 0xff) + 0x100, 16).substring(1);
}
System.out.println("Result:[" + result + "]");
return mac_data;
} catch (Exception _exception) {
_exception.printStackTrace();
}
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
public static void main(String[] args) {
String key = "7F6D7A688019DC4CA83883F2459E6B389BE99BB63B622A97";
String cadena = "1800990000014303181112190600048430303030303134370029030003483032001148324830303030303135320008143919751D5EB97C";
byte[] data_for_mac = hexStringToByteArray(cadena);
System.out.println(new String(data_for_mac));
byte[]mac = calculateMAC(data_for_mac,key);
}
}
What I see printed as a result is: 0e01a8a96b30ae0a918865d1cef898ea4f96e18680fa9aae4c5d9902090c2f81
But the expected value is: 7684024da7fe89029965ac037a1ec94b21479c91cd8495e726fec924e84b0773
I used a couple of online tools that allow me to enter an hexadecimal string as inputo to generate the hash. The result is the same as the expected, so I confirmed that mine has a problem.
I don´t know what I might be doing different or wrong,can anyone please help? Thank you
Upvotes: 1
Views: 676
Reputation: 9173
From your main
function parameters, I'm expecting that key is hex string too. So you need to call hexStringToByteArray
for it too. getBytes()
method just return bytes, it will not convert from hex string.
Change calculateMAC
function to something like this:
public static byte[] calculateMAC(byte[] _aiOutBufferForMacCalculation, byte[] key) {
try{
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key, "HmacSHA256");
...
}
and you main
like this:
public static void main(String[] args) {
String key = "7F6D7A688019DC4CA83883F2459E6B389BE99BB63B622A97";
String cadena = "1800990000014303181112190600048430303030303134370029030003483032001148324830303030303135320008143919751D5EB97C";
byte[] data_for_mac = hexStringToByteArray(cadena);
byte[] mac_key = hexStringToByteArray(key);
// System.out.println(new String(data_for_mac)); <-- what is this????
byte[] mac = calculateMAC(data_for_mac,mac_key);
}
I tested and with these 2 small changes, you get your expected results.
By the way, I'm expecting that you have surely not posted all important key in this sample code and you are just using a dummy one. :D
Upvotes: 1