使用OpenSSL的MD5計算功能:
#include
#include
#include
#include
using namespace std;
int main()
{
FILE *fd=fopen("test","r");
MD5_CTX c;
unsigned char md5[17]={0};
if(fd == NULL)
{
cout << "open failed" << endl;
return -1;
}
int len;
unsigned char *pData = (unsigned char*)malloc(1024*1024*1024);
if(!pData)
{
cout << "malloc failed" << endl;
return -1;
}
MD5_Init(&c);
while( 0 != (len = fread(pData, 1, 1024*1024*1024, fd) ) )
{
MD5_Update(&c, pData, len);
}
MD5_Final(md5,&c);
for(int i = 0; i < 16; i++)
cout << hex << setw(2) << setfill('0') << (int)md5[i];
cout << endl;
fclose(fd);
free(pData);
return 0;
}
OpenSSL 的詳細介紹:請點這裡
OpenSSL 的下載地址:請點這裡
推薦閱讀:
通過OpenSSL提供FTP+SSL/TLS認證功能,並實現安全數據傳輸 http://www.linuxidc.com/Linux/2013-05/84986.htm