Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

c - SHA256 generating same hash value for every input

I am using SHA256 to generate hash value in C within a loop, along with the value for which hash is to be generated the counter variable of loop is also passed. As value of counter variable changes in every iteration so I expect SHA256 to return different hash every time. But it returns same hash every time. Please Help!

Thanks in Advance

Code:

int generate_sha256hash { 
  int loop = 0; 
  unsigned char hash_paramters[2] = {0};
  unsigned char device_ids[2] = {0,0}; 
  hash_paramters[0] = 0; 
  unsigned char pass_string = "PASSWORD"; 

   for(loop = 1; loop < 10; loop++) {
     hash_paramters[1] = loop;
     memcpy((unsigned char)(&input_info[0]),(unsigned char )hash_paramters ,2); 
     memcpy((unsigned char)(&input_info[2]),(unsigned char )device_ids,2);
     memcpy((unsigned char)(&device_info[4]),(unsigned char*)au8defaultPwd,8); 

     printf("
 Generating Hash Value "); 

     hash_value = SHA256(device_info,14,au8HashValue); 
   }
} 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You change input_info in the loop (dependent on the counter), but don't use it for hash generation. You use only device_info).

And I take it that all the casts to unsigned char in the memcpy calls are actually casts to unsigned char*, as they should be. You've got other syntactic peculiarities in your code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...