keepassxc/src/core/PassphraseGenerator.cpp

106 lines
2.5 KiB
C++
Raw Normal View History

/*
* Copyright (C) 2013 Felix Geyer <debfx@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "PassphraseGenerator.h"
#include <math.h>
#include <QFile>
#include <QTextStream>
#include "crypto/Random.h"
#include "core/FilePath.h"
PassphraseGenerator::PassphraseGenerator()
2017-03-04 16:28:41 -05:00
: m_wordCount(0)
, m_separator(' ')
{
const QString path = filePath()->dataPath("wordlists/eff_large.wordlist");
setWordlist(path);
}
double PassphraseGenerator::calculateEntropy(QString passphrase)
{
Q_UNUSED(passphrase);
if (m_wordlist.size() == 0) {
return 0;
}
2017-03-04 16:28:41 -05:00
return log(m_wordlist.size()) / log(2.0) * m_wordCount;
}
2017-03-04 16:28:41 -05:00
void PassphraseGenerator::setWordCount(int wordCount)
{
2017-03-04 16:28:41 -05:00
m_wordCount = wordCount;
}
void PassphraseGenerator::setWordlist(QString path)
{
m_wordlist.clear();
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning("Couldn't load passphrase wordlist.");
return;
}
QTextStream in(&file);
while (!in.atEnd()) {
m_wordlist.append(in.readLine());
}
2017-03-16 19:12:55 -04:00
if (m_wordlist.size() < 4000) {
qWarning("Wordlist too short!");
return;
}
}
void PassphraseGenerator::setWordseparator(QChar separator) {
m_separator = separator;
}
QString PassphraseGenerator::generatePassphrase() const
{
Q_ASSERT(isValid());
// In case there was an error loading the wordlist
if(m_wordlist.length() == 0) {
2017-03-04 16:28:41 -05:00
return QString();
}
2017-03-04 16:28:41 -05:00
QStringList words;
for (int i = 0; i < m_wordCount; i++) {
int wordIndex = randomGen()->randomUInt(m_wordlist.length());
words.append(m_wordlist.at(wordIndex));
}
2017-03-04 16:28:41 -05:00
return words.join(m_separator);
}
bool PassphraseGenerator::isValid() const
{
2017-03-04 16:28:41 -05:00
if (m_wordCount == 0) {
return false;
}
if (m_wordlist.size() < 1000) {
return false;
}
return true;
}