是否可以使用boost:hash函数生成像MD5一样的固定长度的文件内容哈希?
有没有快速的解决方案?
如果不行,最简单的方法是什么?
最佳答案
不,Boost 不实现 MD5。为此使用加密/哈希库。
根据我的经验,CryptoC++ 非常好。
OpenSSL 实现了所有流行的摘要,下面是一个使用 OpenSSL 的示例:
#include <openssl/md5.h>
#include <iostream>
#include <iomanip>
// Print the MD5 sum as hex-digits.
void print_md5_sum(unsigned char* md) {
for(unsigned i=0; i <MD5_DIGEST_LENGTH; i++) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(md[i]);
}
}
#include <string>
#include <vector>
#include <fstream>
int main(int argc, char *argv[]) {
using namespace std;
vector<string> const args(argv+1, argv+argc);
for (auto& fname : args) {
MD5_CTX ctx;
MD5_Init(&ctx);
ifstream ifs(fname, std::ios::binary);
char file_buffer[4096];
while (ifs.read(file_buffer, sizeof(file_buffer)) || ifs.gcount()) {
MD5_Update(&ctx, file_buffer, ifs.gcount());
}
unsigned char digest[MD5_DIGEST_LENGTH] = {};
MD5_Final(digest, &ctx);
print_md5_sum(digest);
std::cout << "\t" << fname << "\n";
}
}
关于c++ - 如何使用 boost::hash 获取文件内容哈希?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31383580/