Hashing with HmacSHA256 returns unexpected result
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 |
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 |
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 '18 at 15:21
gabyly
asked Nov 13 '18 at 6:42
gabylygabyly
32
32
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 '18 at 20:03
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 at 20:03
add a comment |
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 '18 at 20:03
add a comment |
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 '18 at 15:58
answered Nov 13 '18 at 15:25
AfshinAfshin
3,0161625
3,0161625
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 '18 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 '18 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 '18 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 '18 at 20:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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