Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/PassphraseGenerator.cpp')
-rw-r--r--src/core/PassphraseGenerator.cpp43
1 files changed, 23 insertions, 20 deletions
diff --git a/src/core/PassphraseGenerator.cpp b/src/core/PassphraseGenerator.cpp
index 1af614795..88871eb8c 100644
--- a/src/core/PassphraseGenerator.cpp
+++ b/src/core/PassphraseGenerator.cpp
@@ -17,44 +17,45 @@
#include "PassphraseGenerator.h"
-#include <math.h>
+#include <cmath>
#include <QFile>
#include <QTextStream>
#include "crypto/Random.h"
#include "core/FilePath.h"
+const char* PassphraseGenerator::DefaultSeparator = " ";
+const char* PassphraseGenerator::DefaultWordList = "eff_large.wordlist";
+
PassphraseGenerator::PassphraseGenerator()
: m_wordCount(0)
- , m_separator(' ')
+ , m_separator(PassphraseGenerator::DefaultSeparator)
{
- const QString path = filePath()->dataPath("wordlists/eff_large.wordlist");
- setWordList(path);
}
-double PassphraseGenerator::calculateEntropy(QString passphrase)
+double PassphraseGenerator::calculateEntropy(const QString& passphrase)
{
Q_UNUSED(passphrase);
- if (m_wordlist.size() == 0) {
- return 0;
+ if (m_wordlist.isEmpty()) {
+ return 0.0;
}
- return log(m_wordlist.size()) / log(2.0) * m_wordCount;
+ return std::log2(m_wordlist.size()) * m_wordCount;
}
void PassphraseGenerator::setWordCount(int wordCount)
{
if (wordCount > 0) {
- m_wordCount = wordCount;
+ m_wordCount = wordCount;
} else {
// safe default if something goes wrong
- m_wordCount = 7;
+ m_wordCount = DefaultWordCount;
}
-
+
}
-void PassphraseGenerator::setWordList(QString path)
+void PassphraseGenerator::setWordList(const QString& path)
{
m_wordlist.clear();
@@ -75,7 +76,13 @@ void PassphraseGenerator::setWordList(QString path)
}
}
-void PassphraseGenerator::setWordSeparator(QString separator) {
+void PassphraseGenerator::setDefaultWordList()
+{
+ const QString path = filePath()->wordlistPath(PassphraseGenerator::DefaultWordList);
+ setWordList(path);
+}
+
+void PassphraseGenerator::setWordSeparator(const QString& separator) {
m_separator = separator;
}
@@ -89,8 +96,8 @@ QString PassphraseGenerator::generatePassphrase() const
}
QStringList words;
- for (int i = 0; i < m_wordCount; i++) {
- int wordIndex = randomGen()->randomUInt(m_wordlist.length());
+ for (int i = 0; i < m_wordCount; ++i) {
+ int wordIndex = randomGen()->randomUInt(static_cast<quint32>(m_wordlist.length()));
words.append(m_wordlist.at(wordIndex));
}
@@ -103,9 +110,5 @@ bool PassphraseGenerator::isValid() const
return false;
}
- if (m_wordlist.size() < 1000) {
- return false;
- }
-
- return true;
+ return m_wordlist.size() >= 1000;
}