3

我想使用一些加密操作(主要是完整性检查哈希和)。但是,我在查找执行此类操作的文档时遇到问题:

bool read(std::istream &in) {
    hasher hv(in);
    // Do some operations on hv as if it was std::istream
    hash_type h = hv.finish ();
    hash_type h2 = read_hash(in);
    return h == h2;
}

PS。它可能是不同的库,只要它 a) 与 GPL-3 兼容 b) 在 GNU/Linux 上工作

聚苯乙烯。我不坚持使用crypto++,但是我希望具有类似IOStream的行为,以便与其他C++库进行互操作。

4

2 回答 2

6

crypto++ 的FileSource类接受std::istream&了构造函数,所以看起来你已经完成了。

FileSource (std::istream &in, bool pumpAll, 
    BufferedTransformation *attachment=NULL)

编辑

如果您要问how to use a hash function on istream in cryptopp,这是从cryptopp wiki中获取的示例,由我修改以用于istream

#include "sha.h"
#include "files.h"

std::string digest;

CryptoPP::SHA256 hash;

CryptoPP::FileSource(in, true,   // true here means consume all input at once 
   new CryptoPP::HashFilter(hash,
         new CryptoPP::StringSink(digest)));

std::cout << digest << std::endl;

这将读取流in直到 eof,将其通过hash过滤器,最后结果将在digest字符串中输入。

于 2011-01-12T16:59:24.750 回答
0

使用 crypto++实现您自己的 istream 。

于 2011-01-12T13:49:30.650 回答