Browse Source

New password strengh function

pull/1793/head
tux3 10 years ago
parent
commit
fdcd02980d
  1. 60
      src/widget/form/setpassworddialog.cpp
  2. 2
      src/widget/form/setpassworddialog.h

60
src/widget/form/setpassworddialog.cpp

@ -68,42 +68,32 @@ void SetPasswordDialog::onPasswordEdit()
ui->strengthBar->setValue(getPasswordStrength(pswd)); ui->strengthBar->setValue(getPasswordStrength(pswd));
} }
int SetPasswordDialog::getPasswordStrength(QString pswd) int SetPasswordDialog::getPasswordStrength(QString pass)
{ {
// Password strength calculator if (pass.size() < 6)
// Based on code in the Master Password dialog in Firefox return 0;
// (pref-masterpass.js)
// Original code triple-licensed under the MPL, GPL, and LGPL double fscore = 0;
// so is license-compatible with this file QHash<QChar, int> charCounts;
for (QChar c : pass)
const double lengthFactor = reasonablePasswordLength / 8.0; {
int pwlength = (int)(pswd.length() / lengthFactor); charCounts[c]++;
if (pwlength > 5) fscore += 5. / charCounts[c];
pwlength = 5; }
const QRegExp numRxp("[0-9]", Qt::CaseSensitive, QRegExp::RegExp); int variations = -1;
int numeric = (int)(pswd.count(numRxp) / lengthFactor); variations += pass.contains(QRegExp("[0-9]", Qt::CaseSensitive, QRegExp::RegExp)) ? 1 : 0;
if (numeric > 3) variations += pass.contains(QRegExp("[a-z]", Qt::CaseSensitive, QRegExp::RegExp)) ? 1 : 0;
numeric = 3; variations += pass.contains(QRegExp("[A-Z]", Qt::CaseSensitive, QRegExp::RegExp)) ? 1 : 0;
variations += pass.contains(QRegExp("[\\W]", Qt::CaseSensitive, QRegExp::RegExp)) ? 1 : 0;
const QRegExp symbRxp("\\W", Qt::CaseInsensitive, QRegExp::RegExp);
int numsymbols = (int)(pswd.count(symbRxp) / lengthFactor); int score = fscore;
if (numsymbols > 3) score += variations * 10;
numsymbols = 3; score -= 20;
score = std::min(score, 100);
const QRegExp upperRxp("[A-Z]", Qt::CaseSensitive, QRegExp::RegExp); score = std::max(score, 0);
int upper = (int)(pswd.count(upperRxp) / lengthFactor);
if (upper > 3) return score;
upper = 3;
int pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
if (pwstrength < 0)
pwstrength = 0;
if (pwstrength > 100)
pwstrength = 100;
return pwstrength;
} }
QString SetPasswordDialog::getPassword() QString SetPasswordDialog::getPassword()

2
src/widget/form/setpassworddialog.h

@ -30,7 +30,7 @@ public:
explicit SetPasswordDialog(QString body, QString extraButton, QWidget* parent = 0); explicit SetPasswordDialog(QString body, QString extraButton, QWidget* parent = 0);
~SetPasswordDialog(); ~SetPasswordDialog();
QString getPassword(); QString getPassword();
static int getPasswordStrength(QString password); static int getPasswordStrength(QString pass);
private slots: private slots:
void onPasswordEdit(); void onPasswordEdit();

Loading…
Cancel
Save