Skip to content

Commit

Permalink
use win32 crypt api for password generation on windows
Browse files Browse the repository at this point in the history
creates a new function rand in the util class that calls bcryptgenrandom
from the win32 api when running on windows and falls back to qrand on
other platforms
  • Loading branch information
treat1 committed Jan 16, 2017
1 parent ecd3519 commit 045e4bf
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ QString Pass::Generate_b(int length, const QString &charset) {
} else {
if (charset.length() > 0) {
for (int i = 0; i < length; ++i) {
int index = qrand() % charset.length();
int index = Util::rand() % charset.length();
QChar nextChar = charset.at(index);
passwd.append(nextChar);
}
Expand Down
2 changes: 1 addition & 1 deletion src/src.pro
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ win32 {
}
gcc:QMAKE_LFLAGS += -Wl,--dynamicbase -Wl,--nxcompat
msvc:QMAKE_LFLAGS += /DYNAMICBASE /NXCOMPAT
LIBS += -lmpr
LIBS += -lmpr -lbcrypt
} else:macx {
ICON = ../artwork/icon.icns
QMAKE_INFO_PLIST = ../qtpass.plist
Expand Down
11 changes: 11 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QString>
#ifdef Q_OS_WIN
#include <windows.h>
#include <bcrypt.h>
#else
#include <sys/time.h>
#endif
Expand Down Expand Up @@ -176,3 +177,13 @@ void Util::copyDir(const QString src, const QString dest) {
dest + QDir::separator() + file);
}
}

int Util::rand() {
#ifdef Q_OS_WIN
quint32 ret;
BCryptGenRandom(NULL, (PUCHAR)&ret, sizeof(ret), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
return ret%RAND_MAX;
#else
return qrand();
#endif
}
3 changes: 2 additions & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class Util {
static QString getDir(const QModelIndex &index, bool forPass,
const QFileSystemModel &model,
const StoreModel &storeModel);
static void copyDir(const QString src, const QString dest);
static void copyDir(const QString src, const QString dest);\
static int rand();

private:
static void initialiseEnvironment();
Expand Down
4 changes: 4 additions & 0 deletions tests/auto/util/util.pro
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ HEADERS += util.h \

VPATH += ../../../src
INCLUDEPATH += ../../../src

win32 {
LIBS += -lbcrypt
}

0 comments on commit 045e4bf

Please sign in to comment.