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

时间:2018-10-27 10:55:51

标签: c++ cryptography crypto++

我想获取哈希文件。在当前路径中有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 :: StringSinkTemplate&)':无法将参数1从'std :: vector>'转换为'T&'
  4. 说明 '':无法从'初始化列表'转换为'CryptoPP :: FileSource'

`

1 个答案:

答案 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++?

相关问题