openssl的Md5计算笔记


#include <openssl/md5.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <boost/algorithm/string.hpp>
using std::cout ;
using std::endl ;

std::string downgadget::Downloader_c::FileDigest(const std::string &file)
{
	FILE *fd=fopen(file.c_str(),"r");
        if(!fd){
            return std::string("");
        }
        MD5_CTX c;
	unsigned char md[16];
	int len;
	char tmp[3]={'\0'}, md5buf[33]={'\0'};
	unsigned char buffer [1024]={'\0'};
	MD5_Init(&c);
	while( 0 != (len = fread(buffer, 1, 1024, fd) ) )
	{
		  MD5_Update(&c, buffer, len);
	}
	MD5_Final(md,&c);
	for(int i = 0; i < 16; i++)
	{
	sprintf(tmp,"%02X",md[i]);
	strcat(md5buf,tmp);
	}
	std::string tmpStr(md5buf);
	boost::to_lower(tmpStr);
	cout<<tmpStr<<endl;
	fclose(fd);
	return tmpStr;
}






你可能感兴趣的:(openssl的Md5计算笔记)