StringSource

From Crypto++ Wiki

Jump to: navigation, search

StringSource.png

In the Crypto++ pipelining paradigm, StringSources serve as an origin of data. It is a source that extracts data from a string type, from an ASCIIZ string, or from a buffer of bytes.

[edit] Examples

The following examples demonstrate creation of a StringSource.

StringSource s( "Hello World" );
string data;
StringSource s( data );

The following example demonstrates writing a string to a file.

string data;
StringSource s( data, new FileSink( "test.txt" ) );

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

string data;
StringSource( data, new FileSink( "test.txt" ) );

A slightly more complicated example of pipelining is below. Before the string is written to the file, it is HexEncoded.

string s;
StringSource( s, new HexEncoder( new FileSink( "test.txt" ) ) );

Sometimes the pipeline will be coded over multiple lines to aid in readability. The following is equivalent to the previous.

string s;
StringSource( s,
    new HexEncoder(
        new FileSink( "test.txt" )
    ) // HexEncoder
); // StringSource

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

Personal tools