I am trying to figure out how the HMAC SHA-256 hashing algorithm works. I know that we have to use the following:

H (K ^ opad)|| H ((K ^ ipad)|| text)).

The problem that I am facing is I think of basic understanding of the above. As far as I know, SHA-256 takes in “character input”. The key that I am using is of 256 bits in size and that leads (K^ipad) to be a 512 bit number. How do I use that to get the desired hash?

  • 1
    $\begingroup$ SHA-256, like almost every hash algorithm, is defined in terms of bit inputs. Whether those bits happen to represent characters, images, packets, or something else doesn't matter at all. $\endgroup$ Jan 17 '15 at 15:06
  • $\begingroup$ Also note that K^ipad is an XOR, which will not change the length of K. That said, SHA-256 can input of arbitrary length (for practical intents and purposes). $\endgroup$ Oct 21 '15 at 19:47

SHA-256 is defined for inputting and outputting binary data using bits. Most libraries only perform calculations based on byte input instead of bit input though. So in general, SHA-256 operates on bytes, not on characters.

You need to explicitly encode your characters to to bytes using character encoding such as UTF-8 to use SHA-256. Some platforms have an implicit conversion to bytes, or even equate characters with bytes (the char type in C/C++), but it is better to use explicit conversion none-the-less.

Never confuse characters and bytes, it's the source of about half of the problems with cryptography on stackoverflow (as well as countless bugs on system in the field, not just concerning cryptography).

When comparing the implementation of your library, don't test against random sites on the internet; they usually get encoding/decoding wrong as well. Instead compare with test vectors that do make clear which bytes are actually processed.

After you succeed with the official test vectors, you could perform string comparison with known good libraries (Java, OpenSSL etc.) using code for which the string encoding is explicitly specified.

  • $\begingroup$ I tried. I implemented SHA256 and it works like a charm. But when I try to implement this, the answer does not match the one that i get from an online calculator. The values I used for the key and message are: Key: 256'h603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4; Message: abcdefghijklmnopqrstuvwxyzaabcdef; The answer I get from hashing twice is: 256'hd48ea6df738d5791c9ca0d7ece4f0e76006bd01f3a2b917d7e1316515d1ac951; The answer from online calculator is: 256'hc4da6a966d776db66de44e16904069c9feeca90d8f160d443b2fe0fea8dc7d2b; I don't understand what I am doing wrong. $\endgroup$ Jan 17 '15 at 15:50
  • $\begingroup$ Question: Do the initial hash values for the outer hashing remain the ones pre-defined or do we change those to the hash values we get after the inner-hashing? $\endgroup$ Jan 17 '15 at 15:52
  • $\begingroup$ What online calculator, could you point it out to me? What encoding does it use? PS don't prefix 256'h to your code. $\endgroup$
    – Maarten Bodewes
    Jan 17 '15 at 15:56
  • $\begingroup$ I used two of them and both give the same result. link $\endgroup$ Jan 17 '15 at 15:58
  • $\begingroup$ OK, so they perform character encoding for the key value as well, while you are specifying hex characters. This may mean that you are using hex decoded bytes instead. And their sample key is in hex; brilliantly stupid - don't use code from random sites! $\endgroup$
    – Maarten Bodewes
    Jan 17 '15 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.