From Crypto++ Wiki
Jump to navigation Jump to search

In the pipelining paradigm, RandomNumberSource serves as an origin of data.

Sources, filters and sinks are discussed at Pipelining. The pipeline article explains the design and shows you how to use them.

The RandomNumberSource takes a pointer to a BufferedTransformation. Because a pointer is taken, the RandomNumberSource owns the attached transformation, and therefore will destroy it. See ownership for more details.


RandomNumberSource (RandomNumberGenerator &rng,
                    int length,
                    bool pumpAll,
                    BufferedTransformation *attachment=NULL)

rng is a RandomNumberGenerator.

length is the number of bytes to generate.

pumpAll is a flag indicating the source should pump all of its data.

attachment is an attached BufferedTransformation.


The example below places 8 random bytes of data into a StringSink after Hex Encoding. The code uses a StringSink and string to hold sensitive material. Though convenient, the practice is not a very good idea - see Keys and Formats for details.

AutoSeededRandomPool rng;

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