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

此文参考:《应用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)”

posted @ 2012-11-30 17:21  cxun  阅读(3807)  评论(0编辑  收藏  举报