22 #include <openssl/err.h> 23 #include <openssl/rand.h> 29 QueryPerformanceCounter((LARGE_INTEGER*)&nCounter);
32 gettimeofday(&t, NULL);
33 nCounter = (int64_t)(t.tv_sec * 1000000 + t.tv_usec);
42 RAND_add(&nCounter,
sizeof(nCounter), 1.5);
55 static int64_t nLastPerfmon;
56 if (
GetTime() < nLastPerfmon + 10 * 60)
60 std::vector<unsigned char> vData(250000, 0);
62 unsigned long nSize = 0;
63 const size_t nMaxSize = 10000000;
66 ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA,
"Global", NULL, NULL,
begin_ptr(vData), &nSize);
67 if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
69 vData.resize(std::max((vData.size() * 3) / 2, nMaxSize));
71 RegCloseKey(HKEY_PERFORMANCE_DATA);
72 if (ret == ERROR_SUCCESS) {
73 RAND_add(
begin_ptr(vData), nSize, nSize / 100.0);
75 LogPrint(
"rand",
"%s: %lu bytes\n", __func__, nSize);
77 static bool warned =
false;
79 LogPrintf(
"%s: Warning: RegQueryValueExA(HKEY_PERFORMANCE_DATA) failed with code %i\n", __func__, ret);
88 if (RAND_bytes(buf, num) != 1) {
89 LogPrintf(
"%s: OpenSSL RAND_bytes() failed with error: %s\n", __func__, ERR_error_string(ERR_get_error(), NULL));
101 uint64_t nRange = (std::numeric_limits<uint64_t>::max() / nMax) * nMax;
105 }
while (nRand >= nRange);
106 return (nRand % nMax);
126 if (fDeterministic) {
132 }
while (tmp == 0 || tmp == 0x9068ffffU);
136 }
while (tmp == 0 || tmp == 0x464fffffU);
144 fDeterministic(_fDeterministic)
151 }
while (nTmp == 0 || nTmp == 0x9068ffffU);
155 }
while (nTmp == 0 || nTmp == 0x464fffffU);
void RandAddSeedPerfmon()
static int64_t GetPerformanceCounter()
void memory_cleanse(void *ptr, size_t len)
static int LogPrint(const char *category, const char *format)
uint32_t insecure_rand_Rw
V::value_type * begin_ptr(V &v)
void seed_insecure_rand(bool fDeterministic)
void GetRandBytes(unsigned char *buf, int num)
int64_t GetTime()
For unit testing.
InsecureRand(bool _fDeterministic=false)
uint32_t insecure_rand_Rz
uint64_t GetRand(uint64_t nMax)