Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
* Removed unused passwordGenerator estimate function
* Aligned function names between password and passphrase generator
  • Loading branch information
droidmonkey committed Jun 19, 2019
1 parent 97cb7f8 commit 4fbd459
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 73 deletions.
12 changes: 6 additions & 6 deletions src/browser/BrowserAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,18 +278,18 @@ QJsonObject BrowserAction::handleGetLogins(const QJsonObject& json, const QStrin

QJsonObject BrowserAction::handleGeneratePassword(const QJsonObject& json, const QString& action)
{
const QString nonce = json.value("nonce").toString();
const QJsonObject password = browserSettings()->generatePassword();
auto nonce = json.value("nonce").toString();
auto password = browserSettings()->generatePassword();

if (nonce.isEmpty() || password.isEmpty()) {
return QJsonObject();
}

// For backwards compatibility
password["login"] = password["entropy"];

QJsonArray arr;
QJsonObject passwd = password;
// For backwards compatibility, "login" doesn't really make sense here
passwd["login"] = passwd["entropy"];
arr.append(passwd);
arr.append(password);

const QString newNonce = incrementNonce(nonce);

Expand Down
2 changes: 1 addition & 1 deletion src/browser/BrowserSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ QJsonObject BrowserSettings::generatePassword()
} else {
m_passPhraseGenerator.setWordCount(passPhraseWordCount());
m_passPhraseGenerator.setWordSeparator(passPhraseWordSeparator());
password["entropy"] = m_passPhraseGenerator.getCurrentEntropy();
password["entropy"] = m_passPhraseGenerator.estimateEntropy();
password["password"] = m_passPhraseGenerator.generatePassphrase();
}
return password;
Expand Down
7 changes: 5 additions & 2 deletions src/core/PassphraseGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ PassphraseGenerator::PassphraseGenerator()
setDefaultWordList();
}

double PassphraseGenerator::getCurrentEntropy()
double PassphraseGenerator::estimateEntropy(int wordCount)
{
if (m_wordlist.isEmpty()) {
return 0.0;
}
if (wordCount < 1) {
wordCount = m_wordCount;
}

return std::log2(m_wordlist.size()) * m_wordCount;
return std::log2(m_wordlist.size()) * wordCount;
}

void PassphraseGenerator::setWordCount(int wordCount)
Expand Down
2 changes: 1 addition & 1 deletion src/core/PassphraseGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PassphraseGenerator
TITLECASE
};

double getCurrentEntropy();
double estimateEntropy(int wordCount = 0);
void setWordCount(int wordCount);
void setWordList(const QString& path);
void setWordCase(PassphraseWordCase wordCase);
Expand Down
60 changes: 0 additions & 60 deletions src/core/PasswordGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

#include "PasswordGenerator.h"

#include <algorithm>
#include <cmath>
#include <numeric>

#include "crypto/Random.h"
#include <zxcvbn.h>

Expand All @@ -35,62 +31,6 @@ PasswordGenerator::PasswordGenerator()
{
}

double PasswordGenerator::getCurrentEntropy()
{
Q_ASSERT(isValid());

const QVector<PasswordGroup> groups = passwordGroups();

int totalSize = 0;
for (const PasswordGroup& group : groups) {
// Note: Character groups must be non-overlapping
totalSize += group.size();
}
if (!(m_flags & CharFromEveryGroup)) {
return std::log2(totalSize) * m_length;
}

// CharFromEveryGroup means all groups are required
QVector<int> requiredGroups(groups.size());
std::iota(requiredGroups.begin(), requiredGroups.end(), 0);

// Get all subsets of the set of required groups
// From https://stackoverflow.com/a/16310898 by Ronald Rey (https://stackoverflow.com/users/2175684/ronald-rey)
QVector<QVector<int>> subsets;
QVector<int> empty;
subsets.push_back(empty);
for (int i = 0; i < requiredGroups.size(); ++i) {
QVector<QVector<int>> subsetTemp = subsets;
for (int j = 0; j < subsetTemp.size(); ++j) {
subsetTemp[j].push_back(requiredGroups[i]);
}
for (int j = 0; j < subsetTemp.size(); ++j) {
subsets.push_back(subsetTemp[j]);
}
}

// Sort subsets by size ascending
std::sort(subsets.begin(), subsets.end(), [](const QVector<int>& a, const QVector<int>& b) -> bool {
return a.size() < b.size();
});

// Inclusion-exclusion
// Note: This depends on character groups being non-overlapping
double passwordSpace = 0;
int includeExclude = 1;
QVector<QVector<int>>::iterator subset = subsets.begin();
for (int i = 0; i < requiredGroups.size(); ++i, includeExclude *= -1) {
do {
int totalSubsetSize = 0;
for (int groupIndex : *subset) {
totalSubsetSize += groups[groupIndex].size();
}
passwordSpace += includeExclude * pow((totalSize - totalSubsetSize), m_length);
} while ((++subset)->size() == i);
}
return std::log2(passwordSpace);
}

double PasswordGenerator::estimateEntropy(const QString& password)
{
return ZxcvbnMatch(password.toLatin1(), nullptr, nullptr);
Expand Down
1 change: 0 additions & 1 deletion src/core/PasswordGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class PasswordGenerator
public:
PasswordGenerator();

double getCurrentEntropy();
double estimateEntropy(const QString& password);
void setLength(int length);
void setCharClasses(const CharClasses& classes);
Expand Down
3 changes: 1 addition & 2 deletions src/gui/PasswordGeneratorWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ void PasswordGeneratorWidget::updatePasswordStrength(const QString& password)
if (m_ui->tabWidget->currentIndex() == Password) {
entropy = m_passwordGenerator->estimateEntropy(password);
} else {
// TODO see issue #867
entropy = m_dicewareGenerator->getCurrentEntropy();
entropy = m_dicewareGenerator->estimateEntropy();
}

m_ui->entropyLabel->setText(tr("Entropy: %1 bit").arg(QString::number(entropy, 'f', 2)));
Expand Down

0 comments on commit 4fbd459

Please sign in to comment.