mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-01-20 19:51:32 -05:00
111 lines
2.5 KiB
C++
111 lines
2.5 KiB
C++
|
/*
|
||
|
* 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()
|
||
|
: m_length(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;
|
||
|
}
|
||
|
|
||
|
return log(m_wordlist.size()) / log(2.0) * m_length;
|
||
|
}
|
||
|
|
||
|
void PassphraseGenerator::setLength(int length)
|
||
|
{
|
||
|
m_length = length;
|
||
|
}
|
||
|
|
||
|
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());
|
||
|
}
|
||
|
|
||
|
if (m_wordlist.size() < 1000) {
|
||
|
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) {
|
||
|
QString empty;
|
||
|
return empty;
|
||
|
}
|
||
|
|
||
|
QString passphrase;
|
||
|
for (int i = 0; i < m_length; i ++) {
|
||
|
int word_index = randomGen()->randomUInt(m_wordlist.length());
|
||
|
passphrase.append(m_wordlist.at(word_index));
|
||
|
|
||
|
if(i < m_length - 1) {
|
||
|
passphrase.append(m_separator);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return passphrase;
|
||
|
}
|
||
|
|
||
|
bool PassphraseGenerator::isValid() const
|
||
|
{
|
||
|
if (m_length == 0) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (m_wordlist.size() < 1000) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|