diff --git a/src/core/core.cpp b/src/core/core.cpp index c5e4d744d..26115735b 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -34,8 +34,10 @@ #include "util/strongtype.h" #include -#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)) +#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) #include +#else +#include #endif #include #include @@ -667,6 +669,10 @@ void Core::onStarted() { ASSERT_CORE_THREAD; +#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) + qsrand(static_cast(QDateTime::currentMSecsSinceEpoch())); +#endif + // One time initialization stuff QString name = getUsername(); if (!name.isEmpty()) { @@ -795,10 +801,10 @@ void Core::bootstrapDht() } int i = 0; -#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)) - static int j = QRandomGenerator::global()->generate() % listSize; +#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) + static int j = QRandomGenerator::global()->bounded(listSize); #else - static int j = qrand() % listSize; + static int j = static_cast((static_cast(qrand()) / static_cast(RAND_MAX+1l)) * listSize); #endif // i think the more we bootstrap, the more we jitter because the more we overwrite nodes while (i < 2) { @@ -827,7 +833,8 @@ void Core::bootstrapDht() PARSE_ERR(error); } - ++j; + // bootstrap off every 5th node (+ a special case to avoid cycles when listSize % 5 == 0) + j += 5 + !(listSize % 5); ++i; } } diff --git a/src/widget/form/settings/privacyform.cpp b/src/widget/form/settings/privacyform.cpp index 898290e51..4cb7aea89 100644 --- a/src/widget/form/settings/privacyform.cpp +++ b/src/widget/form/settings/privacyform.cpp @@ -23,8 +23,10 @@ #include #include #include -#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)) +#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) #include +#else +#include #endif #include "src/core/core.h" @@ -102,19 +104,16 @@ void PrivacyForm::showEvent(QShowEvent*) void PrivacyForm::on_randomNosapamButton_clicked() { - QTime time = QTime::currentTime(); -#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)) - QRandomGenerator(static_cast(time.msec())); -#else - qsrand(static_cast(time.msec())); -#endif - uint32_t newNospam{0}; - for (int i = 0; i < 4; ++i) -#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)) - newNospam = (newNospam << 8) + (QRandomGenerator::global()->generate() % 256); // Generate byte by byte. For some reason. + +#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) + // guarantees to give a random 32-bit unsigned integer + newNospam = QRandomGenerator::global()->generate(); #else - newNospam = (newNospam << 8) + (qrand() % 256); // Generate byte by byte. For some reason. + qsrand(static_cast(QDateTime::currentMSecsSinceEpoch())); + for (int i = 0; i < 4; ++i) + // Generate byte by byte, as qrand() is guaranteed to have only 15 bits of randomness (RAND_MAX is guaranteed to be 2^15) + newNospam = (newNospam << 8) + (static_cast((static_cast(qrand()) / static_cast(RAND_MAX+1l)) * 256)); #endif core->setNospam(newNospam);