From d1c4ca5eaeb2aa6e1246bd970fdbf9402381b623 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sun, 12 Jul 2020 13:19:26 -0700 Subject: [PATCH] Use uniform_int_distribution for generating PINs --- app/gui/PcView.qml | 2 +- app/gui/computermodel.cpp | 12 ++++++++++++ app/gui/computermodel.h | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/gui/PcView.qml b/app/gui/PcView.qml index e653ddce..6c42b2e1 100644 --- a/app/gui/PcView.qml +++ b/app/gui/PcView.qml @@ -187,7 +187,7 @@ CenteredGridView { } else { if (!model.busy) { - var pin = ("0000" + Math.floor(Math.random() * 10000)).slice(-4) + var pin = computerModel.generatePinString() // Kick off pairing in the background computerModel.pairComputer(index, pin) diff --git a/app/gui/computermodel.cpp b/app/gui/computermodel.cpp index 141070ca..6a44e936 100644 --- a/app/gui/computermodel.cpp +++ b/app/gui/computermodel.cpp @@ -2,6 +2,8 @@ #include +#include + ComputerModel::ComputerModel(QObject* object) : QAbstractListModel(object) {} @@ -135,6 +137,16 @@ void ComputerModel::renameComputer(int computerIndex, QString name) m_ComputerManager->renameHost(m_Computers[computerIndex], name); } +// TODO: Use QRandomGenerator when we drop Qt 5.9 support +QString ComputerModel::generatePinString() +{ + std::uniform_int_distribution dist(0, 9999); + std::random_device rd; + std::mt19937 engine(rd()); + + return QString::asprintf("%04u", dist(engine)); +} + void ComputerModel::pairComputer(int computerIndex, QString pin) { Q_ASSERT(computerIndex < m_Computers.count()); diff --git a/app/gui/computermodel.h b/app/gui/computermodel.h index a0fd5def..7224d51e 100644 --- a/app/gui/computermodel.h +++ b/app/gui/computermodel.h @@ -31,6 +31,8 @@ public: Q_INVOKABLE void deleteComputer(int computerIndex); + Q_INVOKABLE QString generatePinString(); + Q_INVOKABLE void pairComputer(int computerIndex, QString pin); Q_INVOKABLE void wakeComputer(int computerIndex);