I'd like to provide an std::ostream
that may or may not, from the user's point of view, encrypt its contents.
Imagine some random function that uses an std::ostream&
:
void write_stuff( std::ostream& stream ) {
os << "stuff";
}
Whether stuff
is output in cleartext or is encrypted is dependent on how the stream
argument was initialized.
I'm thinking about inheriting from std::basic_streambuf
, but I'm not sure it's the best way to go. I think the ideal scenario is some sort of filter object which is run before the contents are actually output. That way it'd be easy to chain operations, e.g. encrypting and then hex-encoding, which is a benefit that making my own basic_streambuf
does not seem to give (not easily at least).
Any advices?
Thanks.
UPDATE: I followed the strategy laid out by the accepted answer. I found this article on writing a custom streambuf
which performs XOR obfuscation to be extremely helpful as a starting point.