Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HMAC (Hash-based message authentication code) implementation SHA-256 hash function

195 views
Skip to first unread message

Peter Riddersholm

unread,
Apr 1, 2014, 4:27:08 AM4/1/14
to
Hi,

I am desperately trying to make a HMAC SHA-256 function. I make use of the DataHash function kindly provided by Jan Simon here:

http://www.mathworks.com/matlabcentral/fileexchange/31272-datahash

What I need is a function HMAC(Key, Message) that calculates the keyed-hash message authentication code outputted in HEX. I realize my use of uint8 might not be as smooth as it could be, but so far this is what I've done.

I am trying to emulate the pseudo-code from the wiki article on the subject:

http://en.wikipedia.org/wiki/Hash-based_message_authentication_code#Implementation

If I check my output, with various online clients (such as http://www.freeformatter.com/hmac-generator.html#ad-output) I do not get the correct results.

All help is greatly appreciated.

Anyway, here's my code:



function hash = HMAC(Key,Message)

Key_uint8 = uint8(Key);
Message_uint8 = uint8(Message);

Blocksize = 64;

Opt.Method = 'SHA-256';
Opt.Format = 'hex'; % hex HEX double uint8 base64
Opt.Input = 'bin'; % array file bin

if length(Key_uint8) > Blocksize
Key_uint8 = DataHash(Key_uint8,Opt);
elseif length(Key_uint8) < Blocksize
zeropad(1:Blocksize-length(Key_uint8)) = [0];
Key_uint8 = horzcat(Key_uint8,zeropad);
end

i_pad(1:Blocksize) = [54];
o_pad(1:Blocksize) = [92];

i_key_pad = xor(Key_uint8,i_pad);
o_key_pad = xor(Key_uint8,o_pad);

hash_temp = DataHash(horzcat(i_key_pad,Message_uint8),Opt);

HMAC = DataHash(horzcat(o_key_pad,uint8(hash_temp)),Opt);

end

Peter Riddersholm

unread,
Apr 1, 2014, 8:12:08 AM4/1/14
to
Please let me know, if I need to provide more context or if there is any other reason there might be a lack of answers.

Cheers.

Steven Lord

unread,
Apr 1, 2014, 10:34:36 AM4/1/14
to

"Peter Riddersholm " <pe...@dmi.dk> wrote in message
news:lheaen$578$1...@newscl01ah.mathworks.com...
> Please let me know, if I need to provide more context or if there is any
> other reason there might be a lack of answers.

You posted your first message in this thread at 4:27 AM on the east coast of
the United States, where I suspect many of the readers live. You posted this
follow-up less than four hours later, at 8:12 AM. Be patient!


I also recommend that you take a SIMPLE example and step through the
operations, both the pseudocode (or one of the other implementations linked
on the Wikipedia page -- since you can call Java from MATLAB, that would be
one easy alternative) and your implementation in MATLAB (using the debugging
tools.)

http://www.mathworks.com/help/matlab/using-java-libraries-in-matlab.html

http://www.mathworks.com/help/matlab/debugging-code.html

When you find a place where your code gives a different answer than the Java
or other implementation for a given step, figure out why and resolve the
discrepancy. Repeat until your code gives the same result on that first test
case. Repeat with more test cases until you're confident your code is
working correctly.

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Peter Grunnet Wang

unread,
Apr 10, 2014, 2:41:07 PM4/10/14
to
"Steven Lord" <Steve...@mathworks.com> wrote in message <lheips$31$1...@newscl01ah.mathworks.com>...
Thank you for your reply.

I now made a function that calculates the HMAC SHA-256 from the two strings key and message. It can be found here (if anybody finds it interesting):

http://www.mathworks.se/matlabcentral/fileexchange/46182-hmac-hash-message-authentication-code-function-using-sha256
0 new messages