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