Boost logo

Boost Users :

Subject: [Boost-users] Seeding boost::mt19937 with bytes from CryptoGenRandom
From: e m (e4me4m_at_[hidden])
Date: 2010-02-10 22:14:15


I have a C++ program that uses /dev/urandom on Unix systems to seed
boost::mt19937. I'm porting the program to Windows. I can get good, random
seed data on Windows similar to random seed data produced from /dev/urandom
on Unix systems like so:

HCRYPTPROV hProvider = 0;
BYTE randomBytes[8];
CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
CryptGenRandom(hProvider, sizeof(randomBytes), randomBytes);
CryptReleaseContext(hProvider, 0);

However, I'm running into trouble attempting to pass these randomBytes to
boost::mt19937. On Unix I do something similar to this:

uint64_t GetSeed()
{
uint64_t seed;
std::ifstream urandom;
urandom.open("/dev/urandom");
urandom.read(reinterpret_cast<char*> (&seed), sizeof(seed));
urandom.close();
return seed;
}

boost::mt19937 rng(GetSeed());

That works OK on Unix I think. Could anyone suggest how to get the random
data from the Windows CryptoAPI passed to boost::mt19937 as seed data? I
don't normally program Windows systems, so this has been a bit of a
challenge for me.

Thanks for any tips,

Ed



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net