我想获取哈希文件。在当前路径中有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;
我收到此错误
`
答案 0 :(得分:2)
要将代码简化为基本内容:
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
的问题。另请参见How do you iterate through every file/directory recursively in standard C++?