0

I'm attempting to create a solution by which a file has its hash computed, stored in a .txt file, and facilitates checking the original hash value against that of the same file. The else statement simply returning 2 characters, though I'm unsure how to incorporate it within the hash function.

Is there any way to avoid repeating the hash function for else statement? (which it itself I understand has a ton of issues but it's an attempt on my part)..

                //CHECK IF HASH FILE ALREADY EXISTS

                if(access(hashOutBuf, F_OK) == -1) {
                  FILE *ftest=fopen(hashInBuf, "rb");
                  FILE *ftest2=fopen(hashOutBuf, "wt");

                //HASH FUNCTION

                  SHA512_Init (&mdContext);
                  while ((bytes = fread (data, 1, 1024, ftest)) != 0)
                      SHA512_Update (&mdContext, data, bytes);
                  SHA512_Final (c,&mdContext);
                  for(i = 0; i < SHA512_DIGEST_LENGTH; i++){
                    fprintf(ftest2, "%02x", c[i]);
                    printf("%02x", c[i]);
                  }
                  fclose (ftest);
                  fclose (ftest2);
                  fflush (stdout);
                }
                //MY (SORRY) ATTEMPT AT TRYING TO CHECK IF HASH IS IDENTICAL
                else{
                  printf("%02x", c[i]);
                  FILE *ftest2=fopen(hashOutBuf, "r");
                  sprintf(ch1, "%d", c[i]);
                  while (!feof(ftest2)){
                    char ch2[100];
                    fscanf(ftest2, "%s", ch2);
                    if(strcmp(ch2, ch1) == 0){
                      printf("File is identical");
                    }
                    else {
                      printf("File has changed");
                      printf("%s\n", ch1);
                      printf("%s", ch2);
                    }

                  }

Any tips/pointers would be much appreciated, I understand that beyond the hash function it is a bit of a mess - I'm not great at coding so apologies in advance.

*all variables have previously been defined (and work correctly, for the hash function anyhow. Uses openssl.)

Joseph Smith
  • 129
  • 1
  • 8

1 Answers1

0

You appear to be asking how to avoid repeating the code that appears in the “then” statement of an if in the “else” statement. (This different from asking how to avoid repeating the hash function; the hash function will only be evaluated once in either case, not repeated.)

One option is to perform the hash evaluation before the if(access…) statement and remember its results, then use its results inside the “then” statement and inside the “else” statement.

Another option is to create a subroutine that performs the hash evaluation and then call it from inside the “then” statement and inside the “else” statement.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • The idea of placing the hash evaluation before the `if(access...)` statement occurred to me, and I did attempt this, however the `if(access...)` is then led to believe that the file already exists.. Could this be this because of `fprintf(ftest2, "02x", c[i]);` (under the hash evaluation) or because I'm opening the file with `fopen` beforehand? (apologies for the lack of knowledge I have!) – Joseph Smith Apr 09 '19 at 15:16
  • 1
    @JosephSmith: When you open the file, it is created, if it does not already exist. There is no need to output the output file in advance. Just open the input file, compute the checksum, close the input file, and then decide what to do about output. – Eric Postpischil Apr 09 '19 at 15:32