Skip to content

Commit 36d5f4d

Browse files
authored
Merge pull request #969 from deXol/developPortQt6
Change rand calls to QRandomGenerator
2 parents 6bd652c + b38b88b commit 36d5f4d

9 files changed

Lines changed: 35 additions & 11 deletions

File tree

daemon.pro

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ CONFIG -= app_bundle
1111

1212
CONFIG += c++11
1313

14-
INCLUDEPATH += $$PWD/src $$PWD/src/MessageProtocol $$PWD/src/Mooltipass $$PWD/src/Settings $$PWD/src/CyoEncode
14+
INCLUDEPATH += $$PWD/src $$PWD/src/MessageProtocol $$PWD/src/Mooltipass $$PWD/src/Settings $$PWD/src/CyoEncode $$PWD/src/SimpleCrypt
1515

1616
win32 {
1717
LIBS += -lsetupapi -luser32

src/AppGui.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ AppGui::AppGui(int & argc, char ** argv) :
5454

5555
bool AppGui::initialize()
5656
{
57+
#if QT_VERSION < 0x051000
5758
srand(time(NULL));
59+
#endif
5860

5961
QCoreApplication::setOrganizationName("mooltipass");
6062
QCoreApplication::setOrganizationDomain("themooltipass.com");
@@ -736,7 +738,7 @@ void AppGui::checkUpdate(bool displayMessage)
736738

737739
//Recheck in at least 30minutes plus some random time
738740
if (!displayMessage)
739-
QTimer::singleShot(1000 * 60 * 60 * 30 + rand() % 240, [this]() { checkUpdate(false); });
741+
QTimer::singleShot(1000 * 60 * 60 * 30 + Common::getRand() % 240, [this]() { checkUpdate(false); });
740742
}
741743

742744
QString AppGui::getDataDirPath()

src/Common.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,9 @@ QString Common::createUid(QString prefix)
364364
if (!commonUidInit)
365365
{
366366
commonUidInit = true;
367+
#if QT_VERSION < 0x051000
367368
srand(time(NULL));
369+
#endif
368370
}
369371

370372
//try to generate a unique id based on
@@ -373,11 +375,11 @@ QString Common::createUid(QString prefix)
373375
{
374376
s = QStringLiteral("%1%2%3%4%5%6")
375377
.arg(prefix)
376-
.arg(rand())
377-
.arg(rand())
378-
.arg(rand())
379-
.arg(rand())
380-
.arg(rand());
378+
.arg(getRand())
379+
.arg(getRand())
380+
.arg(getRand())
381+
.arg(getRand())
382+
.arg(getRand());
381383
} while (commonExistingUid.contains(s));
382384

383385
return s;
@@ -468,3 +470,12 @@ QByteArray Common::reverse(const QByteArray &array)
468470
}
469471
return res;
470472
}
473+
474+
int Common::getRand()
475+
{
476+
#if QT_VERSION < 0x051000
477+
return rand();
478+
#else
479+
return QRandomGenerator::global()->generate();
480+
#endif
481+
}

src/Common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ class Common
260260

261261
static QByteArray reverse(const QByteArray& array);
262262

263+
static int getRand();
264+
263265
typedef enum
264266
{
265267
MP_Classic = 0,

src/FilesCache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <QList>
55
#include <QVariantHash>
66
#include <QObject>
7-
#include "SimpleCrypt/SimpleCrypt.h"
7+
#include "SimpleCrypt.h"
88

99
class FilesCache : public QObject
1010
{

src/MPDevice_emul.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "MPDevice_emul.h"
2020
#include "MooltipassCmds.h"
2121

22-
#define CLEAN_MEMORY_QBYTEARRAY(d) do {for (int i = 0; i < d.size(); i++){d[i] = rand() % 256;}} while(0);
22+
#define CLEAN_MEMORY_QBYTEARRAY(d) do {for (int i = 0; i < d.size(); i++){d[i] = Common::getRand() % 256;}} while(0);
2323

2424

2525
MPDevice_emul::MPDevice_emul(QObject *parent):
@@ -209,7 +209,7 @@ void MPDevice_emul::platformWrite(const QByteArray &data)
209209
d[0] = 32;
210210
d[1] = commandData;
211211
for (int i = 0; i < 32; i++)
212-
d[2 + i] = rand() % 255;
212+
d[2 + i] = Common::getRand() % 255;
213213
d.resize(64);
214214
sendReadSignal(d);
215215
break;

src/SimpleCrypt/SimpleCrypt.cpp

100644100755
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131
#include <QDateTime>
3232
#include <QCryptographicHash>
3333
#include <QDataStream>
34+
#include "Common.h"
3435

3536
#if QT_VERSION < 0x060000
3637
using DeviceOpenModeFlag = QIODevice;
@@ -44,7 +45,9 @@ SimpleCrypt::SimpleCrypt():
4445
m_protectionMode(ProtectionChecksum),
4546
m_lastError(ErrorNoError)
4647
{
48+
#if QT_VERSION < 0x051000
4749
srand(uint(QDateTime::currentMSecsSinceEpoch() & 0xFFFF));
50+
#endif
4851
}
4952

5053
SimpleCrypt::SimpleCrypt(quint64 key):
@@ -53,7 +56,9 @@ SimpleCrypt::SimpleCrypt(quint64 key):
5356
m_protectionMode(ProtectionChecksum),
5457
m_lastError(ErrorNoError)
5558
{
59+
#if QT_VERSION < 0x051000
5660
srand(uint(QDateTime::currentMSecsSinceEpoch() & 0xFFFF));
61+
#endif
5762
splitKey();
5863
}
5964

@@ -119,7 +124,7 @@ QByteArray SimpleCrypt::encryptToByteArray(QByteArray plaintext)
119124
}
120125

121126
//prepend a random char to the string
122-
char randomChar = char(rand() & 0xFF);
127+
char randomChar = char(Common::getRand() & 0xFF);
123128
ba = randomChar + integrityProtection + ba;
124129

125130
int pos(0);

src/SimpleCrypt/SimpleCrypt.h

100644100755
File mode changed.

tests/tests.pro

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ DEFINES += QT_DEPRECATED_WARNINGS
2929

3030
include (../src/QSimpleUpdater/QSimpleUpdater.pri)
3131

32+
INCLUDEPATH += $$PWD/../src $$PWD/../src/SimpleCrypt
33+
3234
SOURCES += \
35+
../src/Common.cpp \
3336
../src/SimpleCrypt/SimpleCrypt.cpp \
3437
../src/FilesCache.cpp \
3538
../src/DbBackupsTracker.cpp \
@@ -54,6 +57,7 @@ SOURCES += \
5457
TestParseDomain.cpp
5558

5659
HEADERS += \
60+
../src/Common.h \
5761
../src/SimpleCrypt/SimpleCrypt.h \
5862
../src/FilesCache.h \
5963
../src/DbBackupsTracker.h\

0 commit comments

Comments
 (0)