【问题标题】:How to use boost::hash to get the file content hash?如何使用 boost::hash 获取文件内容哈希?
【发布时间】:2015-10-01 17:31:06
【问题描述】:

是否可以使用 boost:hash 函数像 MD5 一样生成固定长度的文件内容哈希?

有没有快速的解决方案?

如果没有,最简单的方法是什么?

【问题讨论】:

    标签: c++ boost hash


    【解决方案1】:

    不,Boost 没有实现 MD5。为此使用加密/哈希库。

    根据我的经验,CryptoC++ 很好。

    OpenSSL 实现了所有流行的摘要,这是一个使用 OpenSSL 的示例:

    Live On Coliru

    #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";
        }
    }
    

    【讨论】:

    • 这只是 linux,这就是我的问题。
    • @joker 加密 c++ 和 OpenSsl 都可以在 windows 上使用。(可能是您梦寐以求的大多数平台)
    • 您的代码(和任何其他示例)使用 linux 标头,如果是,您能否显示 windows 示例?会很棒
    • @Joker。实际上,这些标头主要处理读取文件。您可能可以通过其他方式处理?这里是 a delightfully C++-ified version。你可以看到我在 this live-stream recording 中重构了它。
    • 我目前无法访问 Windows 开发机器。但是,您可以在这里看到我配置 VS2013 以针对 OpenSSL 进行编译:live-stream recording¹ 回答 this older question 时。我碰巧完成了下载 OpenSSL、创建导入库、配置包含和库目录以及诸如此类的全部任务。因此,如果您遇到困难并有耐心,您可以观看 ¹ (experiment)
    【解决方案2】:

    boot 有这样的功能。至少现在:https://www.boost.org/doc/libs/master/libs/uuid/doc/uuid.html

    #include <iostream>
    #include <algorithm>
    #include <iterator>
    #include <boost/uuid/detail/md5.hpp>
    #include <boost/algorithm/hex.hpp>
    
    using boost::uuids::detail::md5;
    
    std::string toString(const md5::digest_type &digest)
    {
        const auto charDigest = reinterpret_cast<const char *>(&digest);
        std::string result;
        boost::algorithm::hex(charDigest, charDigest + sizeof(md5::digest_type), std::back_inserter(result));
        return result;
    }
    
    int main ()
    {
        std::string s;
    
        while(std::getline(std::cin, s)) {
            md5 hash;
            md5::digest_type digest;
    
            hash.process_bytes(s.data(), s.size());
            hash.get_digest(digest);
    
            std::cout << "md5(" << s << ") = " << toString(digest) << '\n';
        }
    
        return 0;
    }
    

    Live Example

    【讨论】:

    猜你喜欢
    • 2013-06-29
    • 1970-01-01
    • 2012-10-24
    • 2021-03-23
    • 2016-04-09
    • 1970-01-01
    • 2020-01-29
    • 2020-11-28
    • 1970-01-01
    相关资源
    最近更新 更多