keepassxc

Форк
0
/
Diceware.cpp 
83 строки · 2.9 Кб
1
/*
2
 *  Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
3
 *
4
 *  This program is free software: you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation, either version 2 or (at your option)
7
 *  version 3 of the License.
8
 *
9
 *  This program is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17

18
#include "Diceware.h"
19

20
#include "Utils.h"
21
#include "core/PassphraseGenerator.h"
22

23
#include <QCommandLineParser>
24

25
const QCommandLineOption Diceware::WordCountOption =
26
    QCommandLineOption(QStringList() << "W"
27
                                     << "words",
28
                       QObject::tr("Word count for the diceware passphrase."),
29
                       QObject::tr("count", "CLI parameter"));
30

31
const QCommandLineOption Diceware::WordListOption =
32
    QCommandLineOption(QStringList() << "w"
33
                                     << "word-list",
34
                       QObject::tr("Wordlist for the diceware generator.\n[Default: EFF English]"),
35
                       QObject::tr("path"));
36

37
Diceware::Diceware()
38
{
39
    name = QString("diceware");
40
    description = QObject::tr("Generate a new random diceware passphrase.");
41
    options.append(Diceware::WordCountOption);
42
    options.append(Diceware::WordListOption);
43
}
44

45
int Diceware::execute(const QStringList& arguments)
46
{
47
    QSharedPointer<QCommandLineParser> parser = getCommandLineParser(arguments);
48
    if (parser.isNull()) {
49
        return EXIT_FAILURE;
50
    }
51

52
    auto& out = Utils::STDOUT;
53
    auto& err = Utils::STDERR;
54

55
    PassphraseGenerator dicewareGenerator;
56

57
    QString wordCount = parser->value(Diceware::WordCountOption);
58
    if (wordCount.isEmpty()) {
59
        dicewareGenerator.setWordCount(PassphraseGenerator::DefaultWordCount);
60
    } else if (wordCount.toInt() <= 0) {
61
        err << QObject::tr("Invalid word count %1").arg(wordCount) << endl;
62
        return EXIT_FAILURE;
63
    } else {
64
        dicewareGenerator.setWordCount(wordCount.toInt());
65
    }
66

67
    QString wordListFile = parser->value(Diceware::WordListOption);
68
    if (!wordListFile.isEmpty()) {
69
        dicewareGenerator.setWordList(wordListFile);
70
    }
71

72
    if (!dicewareGenerator.isValid()) {
73
        // We already validated the word count input so if the generator is invalid, it
74
        // must be because the word list is too small.
75
        err << QObject::tr("The word list is too small (< 1000 items)") << endl;
76
        return EXIT_FAILURE;
77
    }
78

79
    QString password = dicewareGenerator.generatePassphrase();
80
    out << password << endl;
81

82
    return EXIT_SUCCESS;
83
}
84

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.