# Sink

 Documentation #include

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:

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

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