在C++中使用加密流

Mac*_*tka 3 c++ cryptography crypto++

我想使用一些加密操作(主要是整合检查hashsums).但是我在查找执行此类操作的文档时遇到问题:

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;
}
Run Code Online (Sandbox Code Playgroud)

PS.它可能是不同的库,只要它a)是GPL-3兼容的b)适用于GNU/Linux

PPS.我并不坚持使用crypto ++,但是我想要与其他C++库具有类似IOStream的行为,以实现互操作性.

dav*_*vka 6

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

FileSource (std::istream &in, bool pumpAll, 
    BufferedTransformation *attachment=NULL)
Run Code Online (Sandbox Code Playgroud)

编辑

如果你问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;
Run Code Online (Sandbox Code Playgroud)

这将读取流in直到eof,将其传递给hash过滤器,最后结果将在digest字符串中生效.