[crypto] stream(buf) ctor to message_digest

To calculate md5's of large files, I added a ctor to message_digest: template <typename charT, class traits> explicit message_digest(const std::basic_ios<charT, traits> & s) { std::basic_streambuf<charT, traits> * sb = s.rdbuf(); std::streamsize sz = 0; charT buf[block_size]; while( (sz = sb->sgetn(buf, block_size)) != 0 ) { input(buf, sz); } } Its just as easy to do this outside the message_digest class using input(void*,size_t) but it seems that calculating md5 checksums for large files might be common enough to warrant it. This may not be the best impl. I use basic_ios because its the first class in the ios_base hierarchy that provides rdbuf(). Thoughts? Chris

Hi Chris,
Its just as easy to do this outside the message_digest class using input(void*,size_t)
I'm not a fan of this constructor for this reason and 1) because I like minimal class interfaces - the same functionality can be implemented as free function. 2) because it doesn't handle io errors in a useful way, i.e. it is not discernible if the digest is wrong because of an io error (not enough bytes could be read) or because the file is different from what was expected. Also it's generally not a good idea to do expensive operations in a ctor. Reading a file qualifies as expensive.
This may not be the best impl. I use basic_ios because its the first class in the ios_base hierarchy that provides rdbuf().
I would have expected an istream& argument to make clear that an input stream is required. Kevin
participants (2)
-
Chris Fairles
-
Kevin Sopp