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

Generate.cpp « cli « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: eb8fea5e8c2e8c956b0b4b75dcc74e1b4acfeeaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 *  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 "Generate.h"

#include <QCommandLineParser>
#include <QTextStream>

#include "core/PasswordGenerator.h"

Generate::Generate()
{
    name = QString("generate");
    description = QObject::tr("Generate a new random password.");
}

Generate::~Generate()
{
}

int Generate::execute(const QStringList& arguments)
{
    QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
    QTextStream outputTextStream(stdout, QIODevice::WriteOnly);

    QCommandLineParser parser;
    parser.setApplicationDescription(this->description);
    QCommandLineOption len(QStringList() << "L" << "length",
                                QObject::tr("Length of the generated password."),
                                QObject::tr("length"));
    parser.addOption(len);
    QCommandLineOption lower(QStringList() << "l",
                               QObject::tr("Use lowercase characters in the generated password."));
    parser.addOption(lower);
    QCommandLineOption upper(QStringList() << "u",
                               QObject::tr("Use uppercase characters in the generated password."));
    parser.addOption(upper);
    QCommandLineOption numeric(QStringList() << "n",
                               QObject::tr("Use numbers in the generated password."));
    parser.addOption(numeric);
    QCommandLineOption special(QStringList() << "s",
                               QObject::tr("Use special characters in the generated password."));
    parser.addOption(special);
    QCommandLineOption extended(QStringList() << "e",
                               QObject::tr("Use extended ASCII in the generated password."));
    parser.addOption(extended);
    parser.process(arguments);

    const QStringList args = parser.positionalArguments();
    if (!args.isEmpty()) {
        outputTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli generate");
        return EXIT_FAILURE;
    }

    PasswordGenerator passwordGenerator;

    if (parser.value(len).isEmpty()) {
        passwordGenerator.setLength(PasswordGenerator::DefaultLength);
    } else {
        int length = parser.value(len).toInt();
        passwordGenerator.setLength(length);
    }

    PasswordGenerator::CharClasses classes = 0x0;

    if (parser.isSet(lower)) {
        classes |= PasswordGenerator::LowerLetters; 
    }
    if (parser.isSet(upper)) {
        classes |= PasswordGenerator::UpperLetters; 
    }
    if (parser.isSet(numeric)) {
        classes |= PasswordGenerator::Numbers; 
    }
    if (parser.isSet(special)) {
        classes |= PasswordGenerator::SpecialCharacters; 
    }
    if (parser.isSet(extended)) {
        classes |= PasswordGenerator::EASCII; 
    }

    passwordGenerator.setCharClasses(classes);
    passwordGenerator.setFlags(PasswordGenerator::DefaultFlags);

    if (!passwordGenerator.isValid()) {
        outputTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli generate");
        return EXIT_FAILURE;
    }

    QString password = passwordGenerator.generatePassword();
    outputTextStream << password << endl;

    return EXIT_SUCCESS;
}