3

I'd like to use some cryptographic operations (mainly integrty check hashsums). However I have problems with finding documentations of doing operations of such form:

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. It may be different library provided it a) is GPL-3-compatible b) works on GNU/Linux

PPS. I don't insist on crypto++ however I would like to have IOStream-like behaviour for interoperability with other C++ libraries.

Maciej Piechotka
  • 7,028
  • 6
  • 39
  • 61

2 Answers2

6

crypto++'s FileSource class takes std::istream& in the constructor, so it seems that you are done.

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

EDIT

if you are asking how to use a hash function on istream in cryptopp, here's a sample taken from cryptopp wiki, modified by me for use with 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;

This will read the stream in until eof, pass it through a hash filter and finally the result will enf up in the digest string.

davka
  • 13,974
  • 11
  • 61
  • 86
  • But I cannot operate on the filtered date as on the std::istream. – Maciej Piechotka Jan 12 '11 at 17:09
  • @Maciej Piechotka: I think that your question is not clear, then. Aren't you asking how to use cryptopp on streams? In your example you use `std::istream&`. Perhaps you are asking how to use cryptopp's hash functions on streams? See my edit of the answer. – davka Jan 13 '11 at 08:48
  • @davka: I'm asking how to get std::istream from cryptopp. – Maciej Piechotka Jan 13 '11 at 10:05
  • 1
    @Maciej Piechotka: do you mean getting cryptopp **output** in the form of stream? Well, I see 2 options, before you dive into implementing your own stream: 1) use `FileSink` with `iostream`, have cryptopp write to it then rewind it before passing it as `istream` to another library; 2) use `StringSink` and create a `stringstream` with the string it writes to, and pass it on. – davka Jan 13 '11 at 10:59
  • @davka: However it assumes I know the length of the hashed part or read whole stream from the beginning and it does not depend on the hashed content. – Maciej Piechotka Jan 13 '11 at 11:32
  • @Maciej Piechotka: no, both options will pass on **only** the output of cryptopp. However, I think that your question is extremely underspecified - I am still not getting what is it you are trying to achieve. But never mind, as long as you got your answer ;) – davka Jan 13 '11 at 11:50
  • @davka: Say I have an an xml file `789e403049261701b6caef9ccc679374||` I'd like to be able to get a hash of text between `|` but the position of second `|` depends on the content hashed so I need to be able to mark in parser where hashing ends. – Maciej Piechotka Jan 13 '11 at 12:30
0

Implement your own istream using crypto++.

chris
  • 3,986
  • 1
  • 26
  • 32