From Crypto++ Wiki
Jump to: navigation, search


In the pipelining paradigm, StringSinks serve as a destination endpoint for data. StringSinks stores data into any string type derived from std::basic_string.


The following example demonstrates creation of a StringSink.

string s;
StringSink sink( s );

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

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

cout << s << endl;

true indicates the FileSource should porpagate to all filters in the chain. 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, true, new HexEncoder( new StringSink( s ) ) );

cout << s << endl;

From above, 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.

byte data[] = { 0x00, 0x01, 0x02, 0x03 };
string sink;

HexEncoder encoder;
encoder.Attach( new StringSink( sink ) );
encoder.Put( data, sizeof( data ) );

cout << sink << endl;

At times, Crypto++ will require a reference to a BufferedTransformation object. For example, when saving a key to storage. To accomodate, StringSink offers Ref:

AutoSeededRandomPool rng;

// Generate Private Key
DSA::PrivateKey PrivateKey;
PrivateKey.GenerateRandomWithKeySize(rng, 1024);

string encodedPrivateKey;

// DER Encode Keys (PKCS #8)
PrivateKey.Save( StringSink(encodedPrivateKey).Ref() );