i have after alot of troubles finally come up with this code. It calculates the hash of a text file, and adds the hash to the file. Obviously, that changes the hash, so when i run it again i will get another hash.
However, if i just want to get the hash in its current state without changing it - what should i be changing in my code? Is it the "f = fopen (apszArgV[1], "rb+");" who causes the hash to change?
#include <stdio.h>
#include "md5.h"
#define BUFFER_SIZE 1024
void print_hash(char hash[]);
int main (int iArgC, char *apszArgV[])
{
FILE *f;
MD5_CTX ctx;
BYTE byHash[16];
BYTE byBuffer[BUFFER_SIZE];
int iReadBytes;
if (iArgC < 2) {
printf ("Usage: md5_add <file name>\n");
return 1;
}
f = fopen (apszArgV[1], "rb+");
if (f != NULL) {
md5_init(&ctx);
while (!feof(f)) {
iReadBytes = fread(byBuffer, sizeof(BYTE), BUFFER_SIZE, f);
md5_update(&ctx, byBuffer, iReadBytes);
if (iReadBytes < BUFFER_SIZE) break;
}
md5_final(&ctx, byHash);
f = fopen("fil1.txt", "a");
for (int i = 0; i < 15; i++) {
fprintf (f, "%02X", byHash[i]);
}
fprintf(f, "\n");
fclose (f);
}
print_hash(byHash);
}
void print_hash(char hash[])
{
int idx;
for (idx=0; idx < 16; idx++)
printf("%02x",(int)((unsigned char)hash[idx]));
printf("\n");
}
thanks
new to c btw