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.)