diff options
author | Anne Jan Brouwer <brouwer@annejan.com> | 2018-01-02 13:04:29 +0100 |
---|---|---|
committer | Anne Jan Brouwer <brouwer@annejan.com> | 2018-01-02 13:04:29 +0100 |
commit | f14282412b8a82888b71a3268c985709ba9364d3 (patch) | |
tree | 690dffe3438c25b9dd75254db7c6a8b43a7b0907 /src | |
parent | 722e36e1541e9fdf46284e4915e4a77e1088e1d3 (diff) |
Generate Password code from #338 placed
Diffstat (limited to 'src')
-rw-r--r-- | src/pass.cpp | 29 | ||||
-rw-r--r-- | src/pass.h | 5 | ||||
-rw-r--r-- | src/util.cpp | 28 | ||||
-rw-r--r-- | src/util.h | 3 |
4 files changed, 32 insertions, 33 deletions
diff --git a/src/pass.cpp b/src/pass.cpp index 8c464ddc..d9600587 100644 --- a/src/pass.cpp +++ b/src/pass.cpp @@ -2,8 +2,6 @@ #include "debughelper.h" #include "qtpasssettings.h" #include "util.h" -#include <QTextCodec> -#include <map> using namespace std; using namespace Enums; @@ -90,7 +88,7 @@ QString Pass::Generate_b(unsigned int length, const QString &charset) { } } else { if (charset.length() > 0) { - passwd = Util::generateRandomPassword(charset, length); + passwd = generateRandomPassword(charset, length); } else { emit critical( tr("No characters chosen"), @@ -276,3 +274,28 @@ QString Pass::getRecipientString(QString for_file, QString separator, recipients_str += separator + '"' + recipient + '"'; return recipients_str; } + +/* Copyright (C) 2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. + */ + +quint32 Pass::boundedRandom(quint32 bound) { + if (bound < 2) + return 0; + + quint32 randval; + const quint32 max_mod_bound = (1 + ~bound) % bound; + + do + randval = QRandomGenerator::system()->generate(); + while (randval < max_mod_bound); + + return randval % 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()))); + return out; +} @@ -9,7 +9,10 @@ #include <QList> #include <QProcess> #include <QQueue> +#include <QRandomGenerator> #include <QString> +#include <QTextCodec> +#include <map> /*! \class Pass @@ -56,6 +59,8 @@ public: protected: void executeWrapper(PROCESS id, const QString &app, const QStringList &args, bool readStdout = true, bool readStderr = true); + QString generateRandomPassword(const QString &charset, unsigned int length); + quint32 boundedRandom(quint32 bound); virtual void executeWrapper(PROCESS id, const QString &app, const QStringList &args, QString input, diff --git a/src/util.cpp b/src/util.cpp index cf334122..ae65b519 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -177,31 +177,3 @@ void Util::copyDir(const QString src, const QString dest) { dest + QDir::separator() + file); } } - -/* Copyright (C) 2017 Jason A. Donenfeld <Jason@zx2c4.com>. */ - -#include <QRandomGenerator> -#include <QString> - -quint32 boundedRandom(quint32 bound) { - if (bound < 2) - return 0; - - quint32 randval; - const quint32 max_mod_bound = (1 + ~bound) % bound; - - do - randval = QRandomGenerator::system()->generate(); - while (randval < max_mod_bound); - - return randval % bound; -} - -static QString generateRandomPassword(const QString &charset, - unsigned int length) { - QString out; - for (unsigned int i = 0; i < length; ++i) - out.append(charset.at(static_cast<int>( - boundedRandom(static_cast<unsigned int>(charset.length()))))); - return out; -} @@ -22,8 +22,7 @@ public: const QFileSystemModel &model, const StoreModel &storeModel); static void copyDir(const QString src, const QString dest); - static QString generateRandomPassword(const QString &charset, - unsigned int length); + private: static void initialiseEnvironment(); static QProcessEnvironment _env; |