您的位置:首页 > 运维架构

openssl的Md5计算笔记

2016-03-24 15:42 239 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: