Allow defining additional characters for the password generator

See issue #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.
This commit is contained in:
Benedikt Rascher-Friesenhausen 2019-11-16 17:51:56 +01:00 committed by Jonathan White
parent dce9af219f
commit b2c2f42f30
8 changed files with 560 additions and 463 deletions

View File

@ -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();

View File

@ -107,6 +107,8 @@ public:
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();

View File

@ -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)
{
}
@ -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;
@ -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;
@ -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

View File

@ -60,6 +60,7 @@ public:
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;
@ -67,6 +68,7 @@ public:
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;
@ -90,6 +92,7 @@ private:
int m_length;
CharClasses m_classes;
GeneratorFlags m_flags;
QString m_additional;
QString m_excluded;
Q_DISABLE_COPY(PasswordGenerator)

View File

@ -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()));
@ -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());
@ -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());
@ -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());
@ -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();
}
}
@ -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("");
}

File diff suppressed because it is too large Load Diff

View File

@ -29,6 +29,19 @@ void TestPasswordGenerator::initTestCase()
QVERIFY(Crypto::init());
}
void TestPasswordGenerator::testAdditionalChars()
{
PasswordGenerator generator;
QVERIFY(!generator.isValid());
generator.setAdditionalChars("aql");
generator.setLength(2000);
QVERIFY(generator.isValid());
QString password = generator.generatePassword();
QCOMPARE(password.size(), 2000);
QRegularExpression regex(R"(^[aql]+$)");
QVERIFY(regex.match(password).hasMatch());
}
void TestPasswordGenerator::testCharClasses()
{
PasswordGenerator generator;

View File

@ -26,6 +26,7 @@ class TestPasswordGenerator : public QObject
private slots:
void initTestCase();
void testAdditionalChars();
void testCharClasses();
void testLookalikeExclusion();
};