Skip to content

Commit

Permalink
Allow defining additional characters for the password generator
Browse files Browse the repository at this point in the history
See issue keepassxreboot#3271 for a motivation of this feature.

This patch adds an additional text input to the advanced view of the password generator.  All characters of this input field (if not empty) will be added as another group to the password generator.  The characters from the excluded field have precedence over the characters from this new field, meaning any character added to both fields will *not* appear in any generated password.  As the
characters from this new field will be added as their own group to the password generator, checking the 'Include characters from every group' checkbox will
force at least character to be chosen from the new input field.

The `PasswordGenerator` class has also been changed so that the `isValid` method returns `true` if only characters from the new input field would be used.

There is a new, simple test that covers the new feature.  While the test only uses ASCII characters, any Unicode characters can be used with the new feature.
  • Loading branch information
bfrascher authored and droidmonkey committed Mar 10, 2020
1 parent dce9af2 commit b2c2f42
Show file tree
Hide file tree
Showing 8 changed files with 533 additions and 436 deletions.
10 changes: 10 additions & 0 deletions src/browser/BrowserSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,16 @@ void BrowserSettings::setAdvancedMode(bool advancedMode)
config()->set("generator/AdvancedMode", advancedMode);
}

QString BrowserSettings::passwordAdditionalChars()
{
return config()->get("generator/AdditionalChars", PasswordGenerator::DefaultAdditionalChars).toString();
}

void BrowserSettings::setPasswordAdditionalChars(const QString& chars)
{
config()->set("generator/AdditionalChars", chars);
}

QString BrowserSettings::passwordExcludedChars()
{
return config()->get("generator/ExcludedChars", PasswordGenerator::DefaultExcludedChars).toString();
Expand Down
2 changes: 2 additions & 0 deletions src/browser/BrowserSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ class BrowserSettings
void setPasswordUseEASCII(bool useEASCII);
bool advancedMode();
void setAdvancedMode(bool advancedMode);
QString passwordAdditionalChars();
void setPasswordAdditionalChars(const QString& chars);
QString passwordExcludedChars();
void setPasswordExcludedChars(const QString& chars);
int passPhraseWordCount();
Expand Down
18 changes: 17 additions & 1 deletion src/core/PasswordGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@

#include "crypto/Random.h"

const char* PasswordGenerator::DefaultAdditionalChars = "";
const char* PasswordGenerator::DefaultExcludedChars = "";

PasswordGenerator::PasswordGenerator()
: m_length(0)
, m_classes(nullptr)
, m_flags(nullptr)
, m_additional(PasswordGenerator::DefaultAdditionalChars)
, m_excluded(PasswordGenerator::DefaultExcludedChars)
{
}
Expand Down Expand Up @@ -53,6 +55,11 @@ void PasswordGenerator::setFlags(const GeneratorFlags& flags)
m_flags = flags;
}

void PasswordGenerator::setAdditionalChars(const QString& chars)
{
m_additional = chars;
}

void PasswordGenerator::setExcludedChars(const QString& chars)
{
m_excluded = chars;
Expand Down Expand Up @@ -107,7 +114,7 @@ QString PasswordGenerator::generatePassword() const

bool PasswordGenerator::isValid() const
{
if (m_classes == 0) {
if (m_classes == 0 && m_additional.isEmpty()) {
return false;
} else if (m_length == 0) {
return false;
Expand Down Expand Up @@ -259,6 +266,15 @@ QVector<PasswordGroup> PasswordGenerator::passwordGroups() const

passwordGroups.append(group);
}
if (!m_additional.isEmpty()) {
PasswordGroup group;

for (auto ch : m_additional) {
group.append(ch);
}

passwordGroups.append(group);
}

// Loop over character groups and remove excluded characters from them;
// remove empty groups
Expand Down
3 changes: 3 additions & 0 deletions src/core/PasswordGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ class PasswordGenerator
void setLength(int length);
void setCharClasses(const CharClasses& classes);
void setFlags(const GeneratorFlags& flags);
void setAdditionalChars(const QString& chars);
void setExcludedChars(const QString& chars);

bool isValid() const;

QString generatePassword() const;

static const int DefaultLength = 16;
static const char* DefaultAdditionalChars;
static const char* DefaultExcludedChars;
static constexpr bool DefaultLower = (DefaultCharset & LowerLetters) != 0;
static constexpr bool DefaultUpper = (DefaultCharset & UpperLetters) != 0;
Expand All @@ -90,6 +92,7 @@ class PasswordGenerator
int m_length;
CharClasses m_classes;
GeneratorFlags m_flags;
QString m_additional;
QString m_excluded;

Q_DISABLE_COPY(PasswordGenerator)
Expand Down
17 changes: 8 additions & 9 deletions src/gui/PasswordGeneratorWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ PasswordGeneratorWidget::PasswordGeneratorWidget(QWidget* parent)
connect(m_ui->editNewPassword, SIGNAL(textChanged(QString)), SLOT(updatePasswordStrength(QString)));
connect(m_ui->buttonAdvancedMode, SIGNAL(toggled(bool)), SLOT(setAdvancedMode(bool)));
connect(m_ui->buttonAddHex, SIGNAL(clicked()), SLOT(excludeHexChars()));
connect(m_ui->editAdditionalChars, SIGNAL(textChanged(QString)), SLOT(updateGenerator()));
connect(m_ui->editExcludedChars, SIGNAL(textChanged(QString)), SLOT(updateGenerator()));
connect(m_ui->buttonApply, SIGNAL(clicked()), SLOT(applyPassword()));
connect(m_ui->buttonCopy, SIGNAL(clicked()), SLOT(copyPassword()));
Expand Down Expand Up @@ -114,6 +115,8 @@ void PasswordGeneratorWidget::loadSettings()
config()->get("generator/SpecialChars", PasswordGenerator::DefaultSpecial).toBool());
m_ui->checkBoxNumbersAdv->setChecked(
config()->get("generator/Numbers", PasswordGenerator::DefaultNumbers).toBool());
m_ui->editAdditionalChars->setText(
config()->get("generator/AdditionalChars", PasswordGenerator::DefaultAdditionalChars).toString());
m_ui->editExcludedChars->setText(
config()->get("generator/ExcludedChars", PasswordGenerator::DefaultExcludedChars).toString());

Expand Down Expand Up @@ -315,6 +318,7 @@ void PasswordGeneratorWidget::setAdvancedMode(bool state)
{
if (state) {
m_ui->simpleBar->hide();
m_ui->advancedContainer->show();
m_ui->checkBoxUpperAdv->setChecked(m_ui->checkBoxUpper->isChecked());
m_ui->checkBoxLowerAdv->setChecked(m_ui->checkBoxLower->isChecked());
m_ui->checkBoxNumbersAdv->setChecked(m_ui->checkBoxNumbers->isChecked());
Expand All @@ -325,15 +329,9 @@ void PasswordGeneratorWidget::setAdvancedMode(bool state)
m_ui->checkBoxDashes->setChecked(m_ui->checkBoxSpecialChars->isChecked());
m_ui->checkBoxLogograms->setChecked(m_ui->checkBoxSpecialChars->isChecked());
m_ui->checkBoxExtASCIIAdv->setChecked(m_ui->checkBoxExtASCII->isChecked());
m_ui->advancedBar->show();
m_ui->excludedChars->show();
m_ui->checkBoxExcludeAlike->show();
m_ui->checkBoxEnsureEvery->show();
} else {
m_ui->advancedBar->hide();
m_ui->excludedChars->hide();
m_ui->checkBoxExcludeAlike->hide();
m_ui->checkBoxEnsureEvery->hide();
m_ui->simpleBar->show();
m_ui->advancedContainer->hide();
m_ui->checkBoxUpper->setChecked(m_ui->checkBoxUpperAdv->isChecked());
m_ui->checkBoxLower->setChecked(m_ui->checkBoxLowerAdv->isChecked());
m_ui->checkBoxNumbers->setChecked(m_ui->checkBoxNumbersAdv->isChecked());
Expand All @@ -342,7 +340,6 @@ void PasswordGeneratorWidget::setAdvancedMode(bool state)
| m_ui->checkBoxQuotes->isChecked() | m_ui->checkBoxMath->isChecked() | m_ui->checkBoxDashes->isChecked()
| m_ui->checkBoxLogograms->isChecked());
m_ui->checkBoxExtASCII->setChecked(m_ui->checkBoxExtASCIIAdv->isChecked());
m_ui->simpleBar->show();
}
}

Expand Down Expand Up @@ -523,8 +520,10 @@ void PasswordGeneratorWidget::updateGenerator()
m_passwordGenerator->setCharClasses(classes);
m_passwordGenerator->setFlags(flags);
if (m_ui->buttonAdvancedMode->isChecked()) {
m_passwordGenerator->setAdditionalChars(m_ui->editAdditionalChars->text());
m_passwordGenerator->setExcludedChars(m_ui->editExcludedChars->text());
} else {
m_passwordGenerator->setAdditionalChars("");
m_passwordGenerator->setExcludedChars("");
}

Expand Down
Loading

0 comments on commit b2c2f42

Please sign in to comment.