0

I am developing a hmac-sha1 class for my exam. I've a problem when I have to apply sha-1 two times as described in https://en.wikipedia.org/wiki/Hash-based_message_authentication_code.

When I apply sha1 to a string, it returns me a unsigned int [5] with the hash calculated. I want to convert unsigned int [5] into a char [40] with the hash.

For example

unsigned int H[5] = { 67452301, EFCDAB89, 98BADCFE, 10325476, C3D2E1F0 };

// char [40] will be "67452301EFCDAB8998BADCFE10325476C3D2E1F0"

So, I can concatenate it to ipad, then calculate his hash ipad_hash and finally calculate opad+ipad_hash concatenating the two string.

Is it right?

I'm using arduino uno so "unsigned int" is "unsigned long".

This is my test code (it's a mess but i will clean it): http://pastebin.com/jfwBxAp1

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Floyd Crimson
  • 13
  • 1
  • 4

1 Answers1

0

You can do

char hash_cstr[41];
sprintf(hash_cstr, "%08lX%08lX%08lX%08lX%08lX", H[0], H[1], H[2], H[3], H[4])

Make sure you allocate at least 41 chars (40 for the code and 1 for the NULL terminator).

In the format string %08X 08 means pad to 8 characters using 0, so that you get the leading 0s for the middle bytes, and X means hex format using upper case characters. You can use lower case x for lower case characters. Hex format automatically assumes unsigned. You can use lX for 64 bit types.

Sorin
  • 11,863
  • 22
  • 26
  • It doesn't work, if I print H with Serial.print(H[i], HEX) it gaves me "1E4E888AC66F8DD41E00C5A7AC36A32A9950D271" --> correct, if I print hash_cstr it gaves me "0000888A00001E4E00008DD40000C66F0000C5A7" -->wrong. H is an array of unsigned int, hash_cstr is an array of char. – Floyd Crimson Sep 11 '15 at 09:09
  • @FloydCrimson Your compiler has int defined as 16 bits so you only get 2 bytes with X. Try using lX or llX. – Sorin Sep 11 '15 at 09:13
  • I seams that the first 2 byte of the total 4 byte is swapped with the second 2. With the sprintf I can read half of the entire string... – Floyd Crimson Sep 11 '15 at 10:00