BDE 4.14.0 Production release
|
Provide a class to generate arbitrary length seeds for algorithms.
This component provides a class, bslh::SeedGenerator
, which utilizes a user-supplied Random Number Generator (RNG) to generate arbitrary length seeds. The quality of the seeds will only be as good as the quality of the supplied RNG. A cryptographically secure RNG must be supplied in order for SeedGenerator
to produce seeds suitable for a cryptographically secure algorithm.
This class satisfies the requirements for a seed generator, defined in bslh_seededhash.h
. More information can be found in the package level documentation for bslh
(internal users can also find information here {TEAM BDE:USING MODULAR HASHING<GO>})
The (template parameter) type RANDOM_NUM_GEN
shall be a class that provides a type alias result_type
and exposes an operator()
that returns a result of type result_type
. The value returned by operator()
shall be random bits, the quality of which can be defined by RANDOM_NUM_GEN
. RANDOM_NUM_GEN
shall also be default and copy constructible.
This section illustrates intended usage of this component.
Suppose we have a number of hashing algorithms that all require different length seeds. Some require 32 bits, some require 64 bits, some even require 1024 bits. We want to generate all these seeds in the same way, but we do not want to keep manually generating seeds of different sizes for these algorithms. Moreover, we want to be able to use all these algorithms through a general purpose functor. To accomplish this, we give all our algorithm the same interface and supply a seed generator, which can create any size seed that the algorithms require.
First, we write our first hashing algorithm, which accepts a 32-bit seed and returns a 32-bit unsigned int.
Then, we define another hashing algorithm, which accepts a 64-bit seed and returns a 32-bit unsigned int
Next, we define a final hashing algorithm, which accepts a 1024-bit seed and returns a 32-bit unsigned int
Then, we declare our functor, SeededHash
, which will take a seed generator, and be able to run any of our hashing algorithms by generating the correct size seed with the seed generator.
Next, we define our constructor where we actually use bslh::SeedGenerator
. bslh::SeedGenerator
allows us to create arbitrary length seeds to match the requirements of the above declared algorithms.
Now, we generate some data that we want to hash.
Finally, we can hash the data the same way using all of the different hashing algorithms. The seed generator allows us to abstract away the different requirements each algorithm has on seed size. Each algorithm will produce different output because it has been supplied with a different seed.