VC++使用Crypto++库计算文件的MD5值

来源:互联网 发布:java this和super 编辑:程序博客网 时间:2024/05/22 09:50

http://lang.9sssd.com/vcpp/art/1364

VC++使用Crypto++库计算文件的MD5值

2012-12-11 14:49 来源:博客园 作者:cxun 字号:T|T

[摘要]本文介绍VC++使用Crypto++库计算文件的MD5值,并提供简单的示例代码供参考。

VC++使用Crypto++库计算文件的MD5值代码如下:

View Row Code
#include <iostream>using namespace std; #include "md5.h"#include "hex.h"#include "files.h"#pragma comment(lib,"cryptlib.lib") void main(){    CryptoPP::Weak1::MD5 md;    const size_t size = CryptoPP::Weak1::MD5::DIGESTSIZE* 2;    byte buf[size]= {0};    string strPath = "d:\\a.dat";    CryptoPP::FileSource(strPath.c_str(),true,        new CryptoPP::HashFilter(md,        new CryptoPP::HexEncoder(        new CryptoPP::ArraySink(buf, size))));    string strHash = string(reinterpret_cast<constchar*>(buf), size);    std::cout<<strHash.c_str()<<endl;}

在Visual Studio中设置编译器优化代码后,执行速度会比较高,经测试,要比一般的Hash软件还快一点。设置方法如下:Project Properties -> Configuration Properties -> C/C++ -> Optimization -> Optimization中,选择为“Maximize Speed (/O2)”。

原创粉丝点击