Hashing with HmacSHA256 returns unexpected result
up vote
0
down vote
favorite
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));
bytemac = 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
java hash cryptography sha256 hmac
add a comment |
up vote
0
down vote
favorite
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));
bytemac = 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
java hash cryptography sha256 hmac
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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));
bytemac = 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
java hash cryptography sha256 hmac
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));
bytemac = 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
java hash cryptography sha256 hmac
java hash cryptography sha256 hmac
edited Nov 13 at 15:21
asked Nov 13 at 6:42
gabyly
32
32
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
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
That absolutely solved it! Thank you very much, and yes I used a dummy key I got from a random key generator.
– gabyly
Nov 13 at 20:03
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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
That absolutely solved it! Thank you very much, and yes I used a dummy key I got from a random key generator.
– gabyly
Nov 13 at 20:03
add a comment |
up vote
0
down vote
accepted
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
That absolutely solved it! Thank you very much, and yes I used a dummy key I got from a random key generator.
– gabyly
Nov 13 at 20:03
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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
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
edited Nov 13 at 15:58
answered Nov 13 at 15:25
Afshin
2,5601623
2,5601623
That absolutely solved it! Thank you very much, and yes I used a dummy key I got from a random key generator.
– gabyly
Nov 13 at 20:03
add a comment |
That absolutely solved it! Thank you very much, and yes I used a dummy key I got from a random key generator.
– gabyly
Nov 13 at 20:03
That absolutely solved it! Thank you very much, and yes I used a dummy key I got from a random key generator.
– gabyly
Nov 13 at 20:03
That absolutely solved it! Thank you very much, and yes I used a dummy key I got from a random key generator.
– gabyly
Nov 13 at 20:03
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275230%2fhashing-with-hmacsha256-returns-unexpected-result%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown