openssl是密码学一个很重要的库,调用openssl库进行编程可以大大简化代码的复杂度,今天要介绍的是使用openssl库计算文件的md5值,代码如下:
#include <openssl/md5.h>
#pragma comment(lib, "libeay32.lib")
#pragma comment(lib, "ssleay32.lib")
void main()
{
int i,blocklength;
long int filelength;
unsigned char ch[4096], s[64];
char filepath[256];
FILE* fp;
puts("文件md5计算器 by 厄斐琉斯\n");
while(true)
{
printf("请输入文件完整路径名:");
scanf("%s", filepath);
if((fp=fopen(filepath,"rb"))==NULL)
printf("can't open the file\n");
MD5_CTX m;
MD5_Init(&m);
fseek(fp,0L,SEEK_END);
filelength=ftell(fp);
fseek(fp,0L,SEEK_SET);
blocklength = 4096;
while(filelength > blocklength)
{
fread(ch,sizeof(unsigned char),blocklength,fp);
MD5_Update(&m, ch, blocklength);
filelength -= blocklength;
}
fread(ch,sizeof(unsigned char),filelength,fp);
MD5_Update(&m, ch, filelength);
MD5_Final(s, &m);
printf("文件的md5值为:");
for(i=0; i<16; i++)
printf("%02X", s[i]);
printf("\n\n");
}
}
程序使用VC6.0环境编译运行