diff options
author | Brad King <brad.king@kitware.com> | 2011-05-17 14:07:26 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2011-05-17 14:07:26 (GMT) |
commit | e1b0a11dd471e1593ade56897185a9d4dd2e0857 (patch) | |
tree | b0f1efae3ec53531386f148ce75098e40d2b1e96 /Source/cmSystemTools.cxx | |
parent | a77dfb6d64b24737167e4a0312fb842544f27961 (diff) | |
download | CMake-e1b0a11dd471e1593ade56897185a9d4dd2e0857.zip CMake-e1b0a11dd471e1593ade56897185a9d4dd2e0857.tar.gz CMake-e1b0a11dd471e1593ade56897185a9d4dd2e0857.tar.bz2 |
Improve string(RANDOM) default seed
The naive time(0) seed is unique only within one second. Instead try to
read a real source of entropy and otherwise fall back to a combination
of the process id and high-resolution time.
Diffstat (limited to 'Source/cmSystemTools.cxx')
-rw-r--r-- | Source/cmSystemTools.cxx | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 7bc89a4..4167355 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -31,7 +31,9 @@ #if defined(_WIN32) # include <windows.h> +# include <wincrypt.h> #else +# include <sys/time.h> # include <sys/types.h> # include <unistd.h> # include <utime.h> @@ -2233,6 +2235,68 @@ bool cmSystemTools::FileTimeSet(const char* fname, cmSystemToolsFileTime* t) } //---------------------------------------------------------------------------- +#ifdef _WIN32 +static int WinCryptRandom(void* data, size_t size) +{ + int result = 0; + HCRYPTPROV hProvider = 0; + if(CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL, + CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) + { + result = CryptGenRandom(hProvider, (DWORD)size, (BYTE*)data)? 1:0; + CryptReleaseContext(hProvider, 0); + } + return result; +} +#endif + +//---------------------------------------------------------------------------- +unsigned int cmSystemTools::RandomSeed() +{ +#if defined(_WIN32) && !defined(__CYGWIN__) + unsigned int seed = 0; + + // Try using a real random source. + if(WinCryptRandom(&seed, sizeof(seed))) + { + return seed; + } + + // Fall back to the time and pid. + FILETIME ft; + GetSystemTimeAsFileTime(&ft); + unsigned int t1 = static_cast<unsigned int>(ft.dwHighDateTime); + unsigned int t2 = static_cast<unsigned int>(ft.dwLowDateTime); + unsigned int pid = static_cast<unsigned int>(GetCurrentProcessId()); + return t1 ^ t2 ^ pid; +#else + union + { + unsigned int integer; + char bytes[sizeof(unsigned int)]; + } seed; + + // Try using a real random source. + std::ifstream fin("/dev/urandom"); + if(fin && fin.read(seed.bytes, sizeof(seed)) && + fin.gcount() == sizeof(seed)) + { + return seed.integer; + } + + // Fall back to the time and pid. + struct timeval t; + gettimeofday(&t, 0); + unsigned int pid = static_cast<unsigned int>(getpid()); + unsigned int tv_sec = t.tv_sec; + unsigned int tv_usec = t.tv_usec; + // Since tv_usec never fills more than 11 bits we shift it to fill + // in the slow-changing high-order bits of tv_sec. + return tv_sec ^ (tv_usec << 21) ^ pid; +#endif +} + +//---------------------------------------------------------------------------- static std::string cmSystemToolsExecutableDirectory; void cmSystemTools::FindExecutableDirectory(const char* argv0) { |