libmd5地址:https://sourceforge.net/projects/libmd5-rfc/
MD5Demo1.c
#include <stdio.h>
#include <string.h>
#include "md5/md5.h"
int main()
{
const md5_byte_t *data = "All is well!";
md5_byte_t digest[];
md5_state_t md5StateT;
md5_init(&md5StateT);
md5_append(&md5StateT, data, strlen(data));
md5_finish(&md5StateT, digest);
char md5String[] = { '\0' }, hexBuffer[];
for (size_t i = ; i != ; ++i)
{
if (digest[i] < )
sprintf(hexBuffer, "0%X", digest[i]);
else
sprintf(hexBuffer, "%X", digest[i]);
strcat(md5String, hexBuffer);
}
printf("%s\n", md5String);
system("pause");
return ;
}
MD5Demo2.c
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "md5/md5.h"
#define INPUT_BUFFER_SIZE 100 * 1024
int main()
{
const char *filename = "E:/Packages/Win7/en_windows_7_ultimate_with_sp1_x64_dvd_u_677332.iso";
FILE *file = fopen(filename, "rb");
if (file)
{
printf("正在计算MD5值...\n");
time_t startTime = time(NULL);
md5_state_t md5StateT;
md5_init(&md5StateT);
char buffer[INPUT_BUFFER_SIZE];
while (!feof(file))
{
size_t numberOfObjects = fread(buffer, sizeof(char), INPUT_BUFFER_SIZE, file);
md5_append(&md5StateT, buffer, numberOfObjects);
}
md5_byte_t digest[];
md5_finish(&md5StateT, digest);
char md5String[] = { '\0' }, hexBuffer[];
for (size_t i = ; i != ; ++i)
{
if (digest[i] < )
sprintf(hexBuffer, "0%x", digest[i]);
else
sprintf(hexBuffer, "%x", digest[i]);
strcat(md5String, hexBuffer);
}
time_t endTime = time(NULL);
fclose(file);
printf("计算完毕:%s,耗时%u秒\n", md5String, endTime - startTime);
}
else
printf("无法打开文件:%s\n", filename);
system("pause");
return ;
}