Skip to content

Commit

Permalink
Generate Password code from #338 placed
Browse files Browse the repository at this point in the history
  • Loading branch information
annejan committed Jan 2, 2018
1 parent 722e36e commit f142824
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
29 changes: 26 additions & 3 deletions src/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#include "debughelper.h"
#include "qtpasssettings.h"
#include "util.h"
#include <QTextCodec>
#include <map>

using namespace std;
using namespace Enums;
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions src/pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
#include <QList>
#include <QProcess>
#include <QQueue>
#include <QRandomGenerator>
#include <QString>
#include <QTextCodec>
#include <map>

/*!
\class Pass
Expand Down Expand Up @@ -56,6 +59,8 @@ class Pass : public QObject {
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,
Expand Down
28 changes: 0 additions & 28 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 1 addition & 2 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class Util {
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;
Expand Down

0 comments on commit f142824

Please sign in to comment.