From 2c72ed4f9809fc781aa97af43767ee0b7c8ca3b0 Mon Sep 17 00:00:00 2001 From: Anne Jan Brouwer Date: Tue, 2 Jan 2018 13:26:25 +0100 Subject: Qt 5.9 fallback (unix) from #338 --- src/pass.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/pass.cpp b/src/pass.cpp index d9600587..3da49ab8 100644 --- a/src/pass.cpp +++ b/src/pass.cpp @@ -279,15 +279,26 @@ QString Pass::getRecipientString(QString for_file, QString separator, */ quint32 Pass::boundedRandom(quint32 bound) { - if (bound < 2) + if (bound < 2) { return 0; + } quint32 randval; const quint32 max_mod_bound = (1 + ~bound) % bound; - do +#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0) + if (fd == -1) { + assert((fd = open("/dev/urandom", O_RDONLY)) >= 0); + } +#endif + + do { +#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0) + assert(read(fd, &randval, sizeof(randval)) == sizeof(randval)); +#else randval = QRandomGenerator::system()->generate(); - while (randval < max_mod_bound); +#endif + } while (randval < max_mod_bound); return randval % bound; } @@ -295,7 +306,9 @@ quint32 Pass::boundedRandom(quint32 bound) { QString Pass::generateRandomPassword(const QString &charset, unsigned int length) { QString out; - for (unsigned int i = 0; i < length; ++i) - out.append(charset.at(boundedRandom(charset.length()))); + for (unsigned int i = 0; i < length; ++i) { + out.append(charset.at(static_cast( + boundedRandom(static_cast(charset.length()))))); + } return out; } -- cgit v1.2.3