哈希文件递归并保存到矢量Cryptopp中

Hash file recursive and save into vector Cryptopp

本文关键字:Cryptopp 保存 文件 递归 归并 哈希      更新时间:2023-10-16

我想要获取哈希文件。在当前路径中有4个文件。它需要散列并保存到向量输出中,以便稍后执行其他任务。

CryptoPP::SHA256 hash;
std::vector<std::string> output;
for(auto& p : std::experimental::filesystem::recursive_directory_iterator(std::experimental::filesystem::current_path()))
{
if (std::experimental::filesystem::is_regular_file(status(p)))
{
CryptoPP::FileSource(p, true, new CryptoPP::HashFilter(hash, new CryptoPP::HexEncoder(new CryptoPP::StringSink(output))), true);
}
}
for (auto& list : output)
{
std::cout << list << std::endl;
}
getchar();
return 0;

我收到这个错误

  1. 描述没有构造函数"CryptoPP::FileSource::FileSource"的实例与参数列表匹配
  2. 说明构造函数"CryptoPP::StringSinkTemplate::StringSinkTemplate[with T=std::string]"的实例与参数列表不匹配
  3. 说明"CryptoPP::StringSinkTemplate::StringSinkTemplate(const CryptoPP::StringSink Template&(":无法将参数1从"std::vector>"转换为"T&">
  4. 说明":无法从"初始值设定项列表"转换为"CryptoPP::FileSource">

`

将代码简化为基本代码:

std::vector<std::string> output;
FileSource(p, true, new HashFilter(hash, new HexEncoder(new StringSink(output))), true);

Crypto++StringSink接受对std::string的引用,而不是对std::vector<std::string>的引用。另请参阅Crypto++手册中的StringSink

FileSource需要一个文件名,而不是目录名。假设p是一个目录迭代器而不是文件迭代器,我猜一旦您将名称作为C字符串或std::string,就会遇到额外的问题。

你应该使用这样的东西:

std::vector<std::string> output;
std::string str;
std::string fname = p...;
FileSource(fname.c_str(), true, new HashFilter(hash, new HexEncoder(new StringSink(str))), true);
output.push_back(str);

我不知道如何从p中获取文件名,它是一个std::experimental::filesystem::recursive_directory_iterator。这就是为什么代码只显示std::string fname = p...;

你应该再问一个关于filesystem::recursive_directory_iterator的问题。另请参阅如何在标准C++中递归遍历每个文件/目录?