0

i have implement a sha1 hash function. The hash function is printing out the hash value correctly but is not storing the hash value correctly to the int variable. Please help me out, i have been trying for the whole day, but im not able to correct the mistake. Thank you for the help.

This is the function

void hashSentence1(string Message1)

{   



    //sha1 hash is 20 bytes

    unsigned char hash[20];

    unsigned int ihexvalue;
    stringstream str;


   // compute the sha1 of the input, and store it our hash array

   SHA1((unsigned char*)Message1.c_str(), Message1.size(), hash);





   // convert the hash array to hexadecimal values and print out

  cout << "Hash of first message was: ";

  for(int i = 0; i < 20; ++i) 

  {

      cout << hex << (int)hash[i];
      str << hex << (int)hash[i];
      str >> ihexvalue; 

  }

  cout << endl << "ihexvalue " << ihexvalue;


  cout << endl << endl;



 }

This is the output, The int variable should have the same value as the sha1

Message to be hash edbfbeabc123

sha1 - > df7dd80ba924cdef4421d4d73b26323793e24df

ihexvalue -> df

Community
  • 1
  • 1
Bhappy
  • 13
  • 6

1 Answers1

1

Your loop sets and resets the value of ihexvalue every loop iteration. Therefore, after the loop, ihexvalue contains only the final value of hash[19]. You can see this from the output, which matches the end of the hash sequence.*

You would need to think carefully about what you intend in order to fix this. You could pack more digits into an integer with shifting and ORing. However, one integer is not large enough to hold all of the information you have in the hash.

*As noted in comments, your test example had the same value at both the beginning and the end of the hash. Looking more closely, all loop iterations are using the same stringstream str. Under those circumstances, if you find that ihexvalue keeps reading the same value from str, then it would be because str is accumulating what is written to it, while ihexvalue is still just reading the first value from str every time. You would see this as getting constantly the same value in debug inspection.

The fundamental issue is the same either way. Every loop iteration is setting or resetting the value of ihexvalue.

If you used a fresh (or reset) stringstream for each loop iteration, that would mean reading a different value each time. However, that alone would not solve the fundamental problem that the value of ihexvalue was being reset each time.

If you changed to something like an integer (or short integer or unsigned char) vector, you could use .push_back(ihexvalue) to add new values to the end with each loop iteration, and it could grow to be as long as needed.

That said, I would also suggest that you consider why you are going through the round about way of converting it to text to write to the stringstream and then reading the text from the stringstream to convert it back into a numeric representation. Since you already have numeric values in the unsigned char array that you start with, are you really gaining anything by going through the expensive process of going into and out of a stringstream? I don't know the context, but you might find that is a long way around the mountain only to get back to the integer value you started with.

Eric
  • 481
  • 2
  • 10
  • Hi, thanks for your help, but my program is not displaying the final value of hash. Instead it is displaying the first two. I did change my int to int [] but it is still not working. – Bhappy May 09 '16 at 17:47
  • I would recommend including an example in which the first and last hex digit pairs are not the same. I would expect the last value set for ihexvalue to be the value it retains. In your revision to allow for an array (or better yet a vector) of int values, be sure you are reading each iteration's value into a new position. OR if using a vector, you could push_back an integer onto the end of the integer vector. – Eric May 09 '16 at 18:08
  • Besides using a test case where the beginning and ending of the hash differ, an additional way to see what is going on is to (temporarily) write out the value of ihexvalue at the end of each loop iteration -- or simply inspect the value of ihexvalue on each loop iteration in debug mode. – Eric May 09 '16 at 18:37
  • I've added some clarification and elaboration to my answer. If that helpfully answers your posted question, please indicate so by selecting my answer. Thanks! – Eric May 09 '16 at 21:00
  • Hello, thanks for you help. How can i select your answer to be the best – Bhappy May 10 '16 at 03:56
  • "To mark an answer as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in." http://stackoverflow.com/help/someone-answers p.s. Glad to be of help. – Eric May 12 '16 at 00:38