diff --git a/include/opendht/rng.h b/include/opendht/rng.h index 9adf30ab..63e347f0 100644 --- a/include/opendht/rng.h +++ b/include/opendht/rng.h @@ -68,7 +68,7 @@ public: } double entropy() const { - if (has_rdrand or has_rdseed) + if (hasRdrand() or hasRdseed()) return 1.; return 0.; } @@ -76,11 +76,21 @@ public: private: random_device& operator=(random_device&) = delete; - const bool has_rdrand; - const bool has_rdseed; pseudo_engine gen; std::uniform_int_distribution dis {}; + static bool hasIntelCpu(); + static bool hasRdrand() { + static const bool hasrdrand = _hasRdrand(); + return hasrdrand; + } + static bool hasRdseed() { + static const bool hasrdseed = _hasRdseed(); + return hasrdseed; + } + static bool _hasRdrand(); + static bool _hasRdseed(); + struct CPUIDinfo { unsigned int EAX; unsigned int EBX; @@ -88,9 +98,6 @@ private: unsigned int EDX; void get(const unsigned int func, const unsigned int subfunc); }; - static bool hasIntelCpu(); - static bool hasRdrand(); - static bool hasRdseed(); bool rdrandStep(result_type* r); bool rdrand(result_type* r); bool rdseedStep(result_type* r); diff --git a/src/rng.cpp b/src/rng.cpp index 2a8020c1..3e02bbd7 100644 --- a/src/rng.cpp +++ b/src/rng.cpp @@ -37,8 +37,6 @@ namespace dht { namespace crypto { random_device::random_device() : - has_rdrand(hasRdrand()), - has_rdseed(hasRdseed()), gen(std::chrono::system_clock::now().time_since_epoch().count()) {} @@ -47,9 +45,9 @@ random_device::operator()() { result_type prand = dis(gen); result_type hwrand; - if (has_rdseed and rdseed(&hwrand)) + if (hasRdseed() and rdseed(&hwrand)) prand ^= hwrand; - else if (has_rdrand and rdrand(&hwrand)) + else if (hasRdrand() and rdrand(&hwrand)) prand ^= hwrand; return prand; } @@ -78,7 +76,7 @@ random_device::hasIntelCpu() } bool -random_device::hasRdrand() +random_device::_hasRdrand() { if (!hasIntelCpu()) return false; @@ -92,7 +90,7 @@ random_device::hasRdrand() } bool -random_device::hasRdseed() +random_device::_hasRdseed() { if (!hasIntelCpu()) return false;