http://lang.9sssd.com/vcpp/art/1364
VC++使用Crypto++库计算文件的MD5值
2012-12-11 14:49
来源:博客园
作者:cxun
字号:T|T
[摘要]本文介绍VC++使用Crypto++库计算文件的MD5值,并提供简单的示例代码供参考。
VC++使用Crypto++库计算文件的MD5值代码如下:
| #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<const char*>(buf), size); |
| std::cout<<strHash.c_str()<<endl; |
| } |
在Visual Studio中设置编译器优化代码后,执行速度会比较高,经测试,要比一般的Hash软件还快一点。设置方法如下:Project Properties -> Configuration Properties -> C/C++ -> Optimization -> Optimization中,选择为“Maximize Speed (/O2)”。
VC++计算文件MD5值

本文介绍如何使用Crypto++库在VC++中计算文件的MD5值,并提供了示例代码。通过设置编译器优化选项,可以进一步提高计算速度。
409

被折叠的 条评论
为什么被折叠?



