直接上代碼:
#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)”