md5哈希c语言程序,如何在C中计算一个大文件的MD5哈希?

gcc -g -Wall -o文件file.c -lssl -lcrypto

#include

#include

int main()

{

unsigned char c[MD5_DIGEST_LENGTH];

char *filename="file.c";

int i;

FILE *inFile = fopen (filename, "rb");

MD5_CTX mdContext;

int bytes;

unsigned char data[1024];

if (inFile == NULL) {

printf ("%s can't be opened.\n", filename);

return 0;

}

MD5_Init (&mdContext);

while ((bytes = fread (data, 1, 1024, inFile)) != 0)

MD5_Update (&mdContext, data, bytes);

MD5_Final (c,&mdContext);

for(i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", c[i]);

printf (" %s\n", filename);

fclose (inFile);

return 0;

}

结果:

$md5sum file.c

25a904b0e512ee546b3f47574703d9fc file.c

$./file

25a904b0e512ee546b3f47574703d9fc file.c

你可能感兴趣的:(md5哈希c语言程序)