Skip to content

Commit

Permalink
Show character count in password generator dialog (keepassxreboot#10940)
Browse files Browse the repository at this point in the history
Displays the number of characters in the password field in the password generator dialog. This fixes keepassxreboot#10858.
  • Loading branch information
AgostonSzepessy authored Jun 22, 2024
1 parent 80ac50a commit 1f9c25c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
8 changes: 8 additions & 0 deletions share/translations/keepassxc_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6888,6 +6888,14 @@ Do you want to overwrite it?</source>
<source>Special Characters</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>passwordLength</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Characters: %1</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>PasswordWidget</name>
Expand Down
9 changes: 8 additions & 1 deletion src/gui/PasswordGeneratorWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ PasswordGeneratorWidget::PasswordGeneratorWidget(QWidget* parent)

connect(m_ui->editNewPassword, SIGNAL(textChanged(QString)), SLOT(updateButtonsEnabled(QString)));
connect(m_ui->editNewPassword, SIGNAL(textChanged(QString)), SLOT(updatePasswordStrength()));
connect(m_ui->editNewPassword, SIGNAL(textChanged(QString)), SLOT(updatePasswordLengthLabel(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()));
Expand All @@ -80,14 +81,15 @@ PasswordGeneratorWidget::PasswordGeneratorWidget(QWidget* parent)
connect(m_ui->tabWidget, SIGNAL(currentChanged(int)), SLOT(updateGenerator()));
connect(m_ui->wordCaseComboBox, SIGNAL(currentIndexChanged(int)), SLOT(updateGenerator()));

// set font size of password quality and entropy labels dynamically to 80% of
// set font size of password quality, characters, and entropy labels dynamically to 80% of
// the default font size, but make it no smaller than 8pt
QFont defaultFont;
auto smallerSize = static_cast<int>(defaultFont.pointSize() * 0.8f);
if (smallerSize >= 8) {
defaultFont.setPointSize(smallerSize);
m_ui->entropyLabel->setFont(defaultFont);
m_ui->strengthLabel->setFont(defaultFont);
m_ui->passwordLengthLabel->setFont(defaultFont);
}

// set default separator to Space
Expand Down Expand Up @@ -316,6 +318,11 @@ void PasswordGeneratorWidget::updatePasswordStrength()
}
}

void PasswordGeneratorWidget::updatePasswordLengthLabel(const QString& password)
{
m_ui->passwordLengthLabel->setText(tr("Characters: %1").arg(QString::number(password.length())));
}

void PasswordGeneratorWidget::applyPassword()
{
saveSettings();
Expand Down
1 change: 1 addition & 0 deletions src/gui/PasswordGeneratorWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public slots:
private slots:
void updateButtonsEnabled(const QString& password);
void updatePasswordStrength();
void updatePasswordLengthLabel(const QString& password);
void setAdvancedMode(bool advanced);
void excludeHexChars();

Expand Down
46 changes: 45 additions & 1 deletion src/gui/PasswordGeneratorWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>729</width>
<height>427</height>
<height>433</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -59,6 +59,50 @@
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="passwordLengthLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>70</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>30</height>
</size>
</property>
<property name="text">
<string>passwordLength</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTop|Qt::AlignTrailing</set>
</property>
<property name="margin">
<number>3</number>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_6">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="entropyLabel">
<property name="sizePolicy">
Expand Down
3 changes: 3 additions & 0 deletions tests/gui/TestGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,17 +895,20 @@ void TestGui::testPasswordEntryEntropy()
pwGeneratorWidget->findChild<PasswordWidget*>("editNewPassword")->findChild<QLineEdit*>("passwordEdit");
auto* entropyLabel = pwGeneratorWidget->findChild<QLabel*>("entropyLabel");
auto* strengthLabel = pwGeneratorWidget->findChild<QLabel*>("strengthLabel");
auto* passwordLengthLabel = pwGeneratorWidget->findChild<QLabel*>("passwordLengthLabel");

QFETCH(QString, password);
QFETCH(QString, expectedStrengthLabel);

// Dynamically calculate entropy due to variances with zxcvbn wordlists
PasswordHealth health(password);
auto expectedEntropy = QString("Entropy: %1 bit").arg(QString::number(health.entropy(), 'f', 2));
auto expectedPasswordLength = QString("Characters: %1").arg(QString::number(password.length()));

generatedPassword->setText(password);
QCOMPARE(entropyLabel->text(), expectedEntropy);
QCOMPARE(strengthLabel->text(), expectedStrengthLabel);
QCOMPARE(passwordLengthLabel->text(), expectedPasswordLength);

QTest::mouseClick(generatedPassword, Qt::LeftButton);
QTest::keyClick(generatedPassword, Qt::Key_Escape););
Expand Down

0 comments on commit 1f9c25c

Please sign in to comment.