From 845b9ebf81775a50edff2ea45d78caa98644ea9a Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Sun, 12 May 2013 15:13:18 +0200 Subject: [PATCH] Better random seed. --- src/utility/random.cpp | 16 +++++++++++++--- vmime/utility/random.hpp | 4 ---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/utility/random.cpp b/src/utility/random.cpp index 6e96ed4e..40b27b57 100644 --- a/src/utility/random.cpp +++ b/src/utility/random.cpp @@ -31,16 +31,26 @@ namespace vmime { namespace utility { -unsigned int random::m_next(static_cast(::std::time(NULL))); +static unsigned int getRandomSeed() +{ + unsigned int seed; + + platform::getHandler()->generateRandomBytes + (reinterpret_cast (&seed), sizeof(seed)); + + return seed; +} unsigned int random::getNext() { + static unsigned int next = getRandomSeed(); + // Park and Miller's minimal standard generator: // xn+1 = (a * xn + b) mod c // xn+1 = (16807 * xn) mod (2^31 - 1) - m_next = static_cast((16807 * m_next) % 2147483647ul); - return (m_next); + next = static_cast((16807 * next) % 2147483647ul); + return next; } diff --git a/vmime/utility/random.hpp b/vmime/utility/random.hpp index ddf596b1..6c0aabf8 100644 --- a/vmime/utility/random.hpp +++ b/vmime/utility/random.hpp @@ -67,10 +67,6 @@ public: */ static const string getString(const string::size_type length, const string& randomChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); - -protected: - - static unsigned int m_next; };