如何使用Boost计算文件的md5? [英] how to calculate md5 of a file using boost?
本文介绍了如何使用Boost计算文件的md5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要计算文件的md5,但是由于某些原因,我不想将我的项目链接到OpenSSL库.对我来说,最好的解决方案之一是通过boost库来实现.我找到了下一个示例:
I need to calculate md5 of a file, but I don't want to link my project with the OpenSSL library for some reason. For me one of the best solutions is make it via boost library. I found next sample:
#include <boost/md5.hpp>
#include <iostream>
#include <fstream>
std::cout << boost::md5("message").hex_str_value();
boost::md5(std::ifstream("file.txt")).hex_str_value();
但不幸的是,当前Boost版本(1.68.0/1.69.0)中没有 boost/md5.hpp
标头.
but unfortunately there is no boost/md5.hpp
header in current boost version (1.68.0/1.69.0).
有人可以告诉我如何使用当前的Boost 1.69.0版本实现以下示例吗?
Can somebody tell me how to implement the sample below using current boost version 1.69.0?
我希望这篇文章能对所有像我一样搜索它的人有所帮助.
I hope this post will help everyone who will search it as me.
推荐答案
boost具有以下功能:
boost has such functionality:
#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;
}
这篇关于如何使用Boost计算文件的md5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文