我花了最后8个小时写这篇文章:
#include <stdio.h>
#include "md5.h"
#define BUFFER_SIZE 1024
void PrintHash (BYTE byHash[]);
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: print_md5 <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);
printf("File:%s Md5:", apszArgV[1]);
PrintHash (byHash);
printf ("\n");
fclose (f);
}
}
void PrintHash (BYTE byHash[])
{
for (int i = 0; i < 15; i++) {
printf ("%02X", byHash[i]);
}
}
现在我想做的就是将哈希写入我计算过哈希的同一文件的最后一行。
我试过把它放在我调用PrintHash函数的同一行,似乎不起作用,因为每次运行它都会为同一个文件生成一个新的哈希值。
f = fopen(apszArgV[1],"a");
for(i=0; i<16 ;i++) {
fprintf(fp, "%02X", byHash[i]);
}
fclose(fp);
有人可能会指出我做错了吗?
ctw新手