Source

From Crypto++ Wiki
Jump to: navigation, search

Source.png

In the Pipelining paradigm, Sources are the origin of data. They serve the opposite role of a Sink. Crypto++ provides the following stock Sources:

Sources exist for different types of objects (as shown above). A StringSources requires no additional information to originate data. Other objects, such as FileSources require additional information such as a filename. Still others, such as a RandomNumberSource require a RandomNumberGenerator and byte count.

RandomNumberSource.png

Between Sources and Sinks are Filters which perform processing.

Examples

The following example demonstrates creation of a FileSource.

FileSource file( filename );

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 decoded.

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

cout << s << endl;

Note that the HexDecoder and StringSink created with new do not require explicit destruction - the FileSource<tt> will call <tt>delete on the HexDecoder, 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 chaining gets longer, nesting the chaining structure as with if statements offers readability.

AutoSeededRandomPool rng;

RandomNumberSource( rng, 4, true,
   new HexEncoder(
      new ArraySink( s )
   ) // HexEncoder
); // RandomNumberSource

With the type formatting in place, data flow through through the construct is readily apparent.

TransformationFlow.png

Sinks

See Sinks for the corresponding Sinks.