[Botan-devel] Recommended usage of AutoSeeded_RNG

Jack Lloyd lloyd at randombit.net
Tue Oct 13 14:00:37 EDT 2009

On Tue, Oct 13, 2009 at 06:31:10PM +0100, Paul Johnson wrote:
> Hi,
> I'm creating a class that has a number of functions based on the code
> examples. So a number of the functions currently create a local
> AutoSeeded_RNG. I suspect it would make more sense to keep a static
> instance of the class and reuse it, but I'm not sure about how the class
> works, so I thought I should check - are they any general
> recommendations?

A static instance would be trouble, because in C++ there is no real
way to control the order of instantiation. In particular this would
mean the RNG might well be created before LibraryInitializer's
constructor ran, causing untold mayhem, segmentation faults, and
possibly inducing an attack of nose monkeys.

One thing that would work is to define a global pointer and use
an accessor to lazily create it, something like -

RandomNumberGenerator* global_prng = 0;

RandomNumberGenerator& get_global_prng()
      global_prng = new AutoSeeded_RNG;
   return *global_prng;

void delete_global_prng()
   delete global_prng;
   global_prng = 0;

Making this function thread safe is left as an excercise for the

Lastly, the technique I normally use and would recommend is to create
it in main() (or whatever function is the root function for all of
your calls into botan) and pass down a RandomNumberGenerator& where
you need it.


More information about the botan-devel mailing list