Outside Linux Implementations of boost :: random_device
Boost currently only implements a class random_device
for Linux (probably nix). Does anyone know of existing implementations for other OSes? Ideally, these implementations would be open source.
If it doesn't exist, how should I use non-deterministic RNG for Windows as well as Mac OS X? Are there API calls in any environment that provide this functionality? Thanks (and sorry for any questions)!
source to share
Depends on what you want to use for the RNG.
In general terms, you will load seed data into a buffer, generate buffer hash values, mix the counter with the result, and hash it some more. The reason for using a hash function is that good hashes are designed to produce random results from inputs that are more structured.
If you want to use it for crypto, things will get more fun. You will need to jump over more hoops for your RNG to keep repeating patterns within reasonably safe limits. I can recommend Bruce Schneier's Practical Cryptography (for an introduction to RNG and an example implementation). He also got some RNG related stuff about his yarrow RNG.
If boost depends on / dev / random, it most likely works on MacOS as well (as it is).
Windows has CryptoAPI as part of the OS, and this provides RNG quality encryption.
Also, I believe modern Intel processors have hardware RNG on a chip - however, you'll have to figure out how to do this on each OS. Using a higher level API is probably better.
edit: Here's a link on how Intel RNG works
source to share
OpenSSL has a decent one.
#include <openssl/rand.h>
...
time_t now = time(NULL);
RAND_seed(&now, sizeof(now)); // before first number you need
int success = RAND_bytes(...);
if (!success) die_loudly();
RAND_cleanup(); // after you don't need any more numbers
Microsoft CryptoAPI has one on Win32. This requires a few more function calls. Not including the details here because there are 2 to 5 arguments for each of these calls. Be careful, CryptoAPI seems to require the user to have a full local profile (C: \ Documents and Settings \ user \ Local Settings) properly configured before it can give you a random number.
CryptAcquireContext // see docs
CryptGenRandom
CryptReleaseContext
source to share