Bhappy
Bhappy

Reputation: 13

Storing hash value to int

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

Upvotes: 0

Views: 1245

Answers (1)

Eric
Eric

Reputation: 503

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.

Upvotes: 1

Related Questions