Sink

From Crypto++ Wiki
Jump to navigation Jump to search
Sink
Documentation
#include <cryptopp/simple.h>

In the Pipelining paradigm, a Sink is the destination of transformed data. They accept transformed data from Filters, and they are the last member of a chain. Sinks serve the opposite role of Sources.

Sinks exist for different types of objects. Crypto++ provides the following stock Sinks:

You can swap different Sinks in and out. If you are writting data to a file through a FileSink, then you can swap in a StringSink to write the data to memory.

A StringSink requires no additional information to terminate the data chain. Other objects, such as FileSinks require additional information such as a filename.

An ArraySink is just a typedef for a StringSink using the {ptr, size} constructor.

Private keys and other sensitive material should not be save to a string using a StringSink. Many sample code uses a and snippets use StringSink and string to hold the sensitive material. Though convenient, the practice is not a very good idea - see Keys and Formats for details.

Examples

The following example demonstrates the creation of a StringSink.

string s;
StringSink sink( s );

The following example demonstrates reading a file, and placing the contents of the file in a string. This is known as pipelining.

string s;
FileSource file( filename, new StringSink( s ) );

cout << s << endl;

The following example performs the same operation as above, but without the variable file.

string s;
FileSource( filename, true, new StringSink( s ) );

cout << s << endl;

A slightly more complicated example of pipelining is below. Before the FileSource is placed in the string, it is hex encoded.

string s;
FileSource( filename, new HexEncoder( new StringSink( s ) ) );

cout << s << endl;

Note that the HexEncoder and StringSink created with new do not require explicit destruction - the FileSource will call delete on the HexEncoder, which in turns calls delete on the StringSink when it (the FileSource) is destroyed.

Finally, the example below places 4 random bytes of data into a StringSink after hex encoding using a random number source. As the chain gets longer, nesting the chaining structure as with if statements offers better readability.

string s;
AutoSeededRandomPool rng;

RandomNumberSource( rng, 4, true,
   new HexEncoder(
      new CryptoPP::StringSink( s )
   ) // HexEncoder
); // RandomNumberSource

Downloads

No downloads available.