2018-01-19 15:48:40 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
|
|
|
|
*
|
|
|
|
* 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 <cstdlib>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "Diceware.h"
|
|
|
|
|
|
|
|
#include <QCommandLineParser>
|
|
|
|
|
2018-10-28 19:55:00 +01:00
|
|
|
#include "cli/TextStream.h"
|
2018-01-19 15:48:40 +01:00
|
|
|
#include "core/PassphraseGenerator.h"
|
2018-09-29 19:00:47 +02:00
|
|
|
#include "Utils.h"
|
2018-01-19 15:48:40 +01:00
|
|
|
|
|
|
|
Diceware::Diceware()
|
|
|
|
{
|
2018-02-06 01:17:36 +01:00
|
|
|
name = QString("diceware");
|
|
|
|
description = QObject::tr("Generate a new random diceware passphrase.");
|
2018-01-19 15:48:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Diceware::~Diceware()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-06 01:17:36 +01:00
|
|
|
int Diceware::execute(const QStringList& arguments)
|
2018-01-19 15:48:40 +01:00
|
|
|
{
|
2018-10-28 19:55:00 +01:00
|
|
|
TextStream in(Utils::STDIN, QIODevice::ReadOnly);
|
|
|
|
TextStream out(Utils::STDOUT, QIODevice::WriteOnly);
|
2018-01-19 15:48:40 +01:00
|
|
|
|
|
|
|
QCommandLineParser parser;
|
2018-09-29 19:00:47 +02:00
|
|
|
parser.setApplicationDescription(description);
|
|
|
|
QCommandLineOption words(QStringList() << "W" << "words",
|
2018-03-31 16:01:30 -04:00
|
|
|
QObject::tr("Word count for the diceware passphrase."),
|
2018-09-29 19:00:47 +02:00
|
|
|
QObject::tr("count", "CLI parameter"));
|
2018-01-31 00:50:02 +01:00
|
|
|
parser.addOption(words);
|
2018-09-29 19:00:47 +02:00
|
|
|
QCommandLineOption wordlistFile(QStringList() << "w" << "word-list",
|
2018-03-31 16:01:30 -04:00
|
|
|
QObject::tr("Wordlist for the diceware generator.\n[Default: EFF English]"),
|
|
|
|
QObject::tr("path"));
|
2018-01-19 15:48:40 +01:00
|
|
|
parser.addOption(wordlistFile);
|
2018-09-29 19:00:47 +02:00
|
|
|
parser.addHelpOption();
|
2018-01-19 15:48:40 +01:00
|
|
|
parser.process(arguments);
|
|
|
|
|
|
|
|
const QStringList args = parser.positionalArguments();
|
2018-02-06 01:17:36 +01:00
|
|
|
if (!args.isEmpty()) {
|
2018-09-29 19:00:47 +02:00
|
|
|
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli diceware");
|
2018-01-19 15:48:40 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PassphraseGenerator dicewareGenerator;
|
|
|
|
|
2018-01-31 00:50:02 +01:00
|
|
|
if (parser.value(words).isEmpty()) {
|
|
|
|
dicewareGenerator.setWordCount(PassphraseGenerator::DefaultWordCount);
|
|
|
|
} else {
|
|
|
|
int wordcount = parser.value(words).toInt();
|
|
|
|
dicewareGenerator.setWordCount(wordcount);
|
|
|
|
}
|
2018-01-19 15:48:40 +01:00
|
|
|
|
|
|
|
if (!parser.value(wordlistFile).isEmpty()) {
|
|
|
|
dicewareGenerator.setWordList(parser.value(wordlistFile));
|
|
|
|
} else {
|
|
|
|
dicewareGenerator.setDefaultWordList();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dicewareGenerator.isValid()) {
|
2018-09-29 19:00:47 +02:00
|
|
|
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli diceware");
|
2018-01-19 15:48:40 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2018-03-31 16:01:30 -04:00
|
|
|
|
2018-01-19 15:48:40 +01:00
|
|
|
QString password = dicewareGenerator.generatePassphrase();
|
2018-09-29 19:00:47 +02:00
|
|
|
out << password << endl;
|
2018-01-19 15:48:40 +01:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|