0

My program produces a different hash code for each variable, and I don't know why.

   char check[] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
   char pw[5];
   string checked_hash;
   int n = strlen(check);

for(int i = 0; i<n; i++ ) //First for loop
  {
 printf("Inside for loop for %d\n", i);
      pw[0] = check[i];
      checked_hash = crypt(pw, salt);
      puts(checked_hash);

      if( strcmp(checked_hash, user_hash) == 0)
      {
       printf("If executed\n");
          puts(pw);
          break;
      }
  }

The hash code for 'a' has been - 50xeUgkHFeCN2, 50ljncb0HjhsY, 50hxLlS7rvX2Q, 50Bn.g7wo8tuo and other codes completely random. Where am I going wrong?

1

char pw[5];

pw[0] = check[i];

crypt(pw, salt);

at this point, pw is not a string. A string is a char array terminating in the null char. You've set pw[0] to 'a' but the next 4 chars in pw are still garbage values, so using pw in crypt will give you unreliable results.

Be sure to null-terminate any array that you want to treat as a string.

1
  • Thanks a lot. I added '\0' to the next index each time, and it solved the problem. Jan 24 '18 at 20:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .