summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnne Jan Brouwer <brouwer@annejan.com>2018-01-02 13:26:25 +0100
committerAnne Jan Brouwer <brouwer@annejan.com>2018-01-02 13:26:25 +0100
commit2c72ed4f9809fc781aa97af43767ee0b7c8ca3b0 (patch)
treeeb3c1695315a83ef712a8ef011124dc2f71cdf55
parentf14282412b8a82888b71a3268c985709ba9364d3 (diff)
Qt 5.9 fallback (unix) from #338
-rw-r--r--src/pass.cpp23
1 files 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<int>(
+ boundedRandom(static_cast<quint32>(charset.length())))));
+ }
return out;
}