Better random seed.

This commit is contained in:
Vincent Richard 2013-05-12 15:13:18 +02:00
parent ee68f6c06f
commit 845b9ebf81
2 changed files with 13 additions and 7 deletions

View File

@ -31,16 +31,26 @@ namespace vmime {
namespace utility { namespace utility {
unsigned int random::m_next(static_cast<unsigned int>(::std::time(NULL))); static unsigned int getRandomSeed()
{
unsigned int seed;
platform::getHandler()->generateRandomBytes
(reinterpret_cast <unsigned char*>(&seed), sizeof(seed));
return seed;
}
unsigned int random::getNext() unsigned int random::getNext()
{ {
static unsigned int next = getRandomSeed();
// Park and Miller's minimal standard generator: // Park and Miller's minimal standard generator:
// xn+1 = (a * xn + b) mod c // xn+1 = (a * xn + b) mod c
// xn+1 = (16807 * xn) mod (2^31 - 1) // xn+1 = (16807 * xn) mod (2^31 - 1)
m_next = static_cast<unsigned int>((16807 * m_next) % 2147483647ul); next = static_cast<unsigned int>((16807 * next) % 2147483647ul);
return (m_next); return next;
} }

View File

@ -67,10 +67,6 @@ public:
*/ */
static const string getString(const string::size_type length, const string& randomChars static const string getString(const string::size_type length, const string& randomChars
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
protected:
static unsigned int m_next;
}; };