Skip to content

Commit

Permalink
Add PasswordHealth ctor with password only (no database)
Browse files Browse the repository at this point in the history
Some target systems don't like to automatically convert from
nullptr to QSharedPointer<Database>. Works on my system,
unfortunately.
  • Loading branch information
wolframroesler committed Dec 8, 2019
1 parent 1096e19 commit 4c88902
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/browser/BrowserSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ QJsonObject BrowserSettings::generatePassword()
m_passwordGenerator.setCharClasses(passwordCharClasses());
m_passwordGenerator.setFlags(passwordGeneratorFlags());
const QString pw = m_passwordGenerator.generatePassword();
password["entropy"] = PasswordHealth(nullptr, pw).entropy();
password["entropy"] = PasswordHealth(pw).entropy();
password["password"] = pw;
} else {
m_passPhraseGenerator.setWordCount(passPhraseWordCount());
Expand Down
2 changes: 1 addition & 1 deletion src/cli/Estimate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static void estimate(const char* pwd, bool advanced)

int len = static_cast<int>(strlen(pwd));
if (!advanced) {
const auto e = PasswordHealth(nullptr, pwd).entropy();
const auto e = PasswordHealth(pwd).entropy();
// clang-format off
out << QObject::tr("Length %1").arg(len, 0) << '\t'
<< QObject::tr("Entropy %1").arg(e, 0, 'f', 3) << '\t'
Expand Down
5 changes: 5 additions & 0 deletions src/core/PasswordHealth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ PasswordHealth::PasswordHealth(double entropy)
}
}

PasswordHealth::PasswordHealth(QString pwd)
: PasswordHealth(ZxcvbnMatch(pwd.toLatin1(), nullptr, nullptr))
{
}

PasswordHealth::PasswordHealth(QSharedPointer<Database> db, QString pwd, Cache* cache)
: PasswordHealth(ZxcvbnMatch(pwd.toLatin1(), nullptr, nullptr))
{
Expand Down
1 change: 1 addition & 0 deletions src/core/PasswordHealth.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class PasswordHealth
*/
PasswordHealth() = default;
explicit PasswordHealth(double entropy);
explicit PasswordHealth(QString pwd);
PasswordHealth(QSharedPointer<Database> db, QString pwd, Cache* cache = nullptr);
PasswordHealth(QSharedPointer<Database> db, const Entry& entry, Cache* cache = nullptr);

Expand Down
2 changes: 1 addition & 1 deletion src/gui/PasswordGeneratorWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ void PasswordGeneratorWidget::updatePasswordStrength(const QString& password)
{
PasswordHealth health;
if (m_ui->tabWidget->currentIndex() == Password) {
health = PasswordHealth(nullptr, password);
health = PasswordHealth(password);
} else {
health = PasswordHealth(m_dicewareGenerator->estimateEntropy());
}
Expand Down
10 changes: 5 additions & 5 deletions tests/TestPasswordHealth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,39 @@ void TestPasswordHealth::initTestCase()

void TestPasswordHealth::testNoDb()
{
const auto empty = PasswordHealth(nullptr, "");
const auto empty = PasswordHealth("");
QCOMPARE(empty.score(), 0);
QCOMPARE(empty.entropy(), 0.0);
QCOMPARE(empty.quality(), PasswordHealth::Quality::bad);
QVERIFY(empty.color() != QColor());
QVERIFY(!empty.reason().isEmpty());
QVERIFY(!empty.details().isEmpty());

const auto poor = PasswordHealth(nullptr, "secret");
const auto poor = PasswordHealth("secret");
QCOMPARE(poor.score(), 6);
QCOMPARE(int(poor.entropy()), 6);
QCOMPARE(poor.quality(), PasswordHealth::Quality::poor);
QVERIFY(poor.color() != QColor());
QVERIFY(!poor.reason().isEmpty());
QVERIFY(!poor.details().isEmpty());

const auto weak = PasswordHealth(nullptr, "Yohb2ChR4");
const auto weak = PasswordHealth("Yohb2ChR4");
QCOMPARE(weak.score(), 47);
QCOMPARE(int(weak.entropy()), 47);
QCOMPARE(weak.quality(), PasswordHealth::Quality::weak);
QVERIFY(weak.color() != QColor());
QVERIFY(!weak.reason().isEmpty());
QVERIFY(!weak.details().isEmpty());

const auto good = PasswordHealth(nullptr, "MIhIN9UKrgtPL2hp");
const auto good = PasswordHealth("MIhIN9UKrgtPL2hp");
QCOMPARE(good.score(), 78);
QCOMPARE(int(good.entropy()), 78);
QCOMPARE(good.quality(), PasswordHealth::Quality::good);
QVERIFY(good.color() != QColor());
QVERIFY(good.reason().isEmpty());
QVERIFY(good.details().isEmpty());

const auto excellent = PasswordHealth(nullptr, "prompter-ream-oversleep-step-extortion-quarrel-reflected-prefix");
const auto excellent = PasswordHealth("prompter-ream-oversleep-step-extortion-quarrel-reflected-prefix");
QCOMPARE(excellent.score(), 164);
QCOMPARE(int(excellent.entropy()), 164);
QCOMPARE(excellent.quality(), PasswordHealth::Quality::excellent);
Expand Down

0 comments on commit 4c88902

Please sign in to comment.