我有很多麻煩後終於拿出這個代碼。它計算文本文件的散列,並將散列添加到文件中。顯然,這改變了散列,所以當我再次運行它時,我會得到另一個散列。我該如何修改這個程序,以便在不修改文件的情況下計算散列?
但是,如果我只是想在當前狀態下散而不改變它 - 我應該怎麼可以在我的代碼改變?它是「f = fopen(apszArgV [1],」rb +「);」誰導致哈希改變?
#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");
}
感謝
新的C BTW
你還沒有解決[你以前的問題]中的任何內容(https://stackoverflow.com/questions/46670913/how-do-i-write-this-hash-array-to-a-text-file)。 –
不是代碼,沒有。但我問這個問題的方式。我瞭解到我得到不同哈希的原因是因爲將散列打印到文件的過程本身正在改變散列本身(duh)。但現在我的問題是,我怎麼才能在不修改文件的情況下獲得散列? – Hi7651
那麼,你怎麼樣纔不修改文件? –