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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
/*
*
* keygendialog.cpp
*
* Copyright 2008 gpg4usb-team <[email protected]>
*
* This file is part of gpg4usb.
*
* Gpg4usb 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 3 of the License, or
* (at your option) any later version.
*
* Gpg4usb 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 gpg4usb. If not, see <http://www.gnu.org/licenses/>
*/
#include "keygendialog.h"
KeyGenDialog::KeyGenDialog(GpgME::GpgContext *ctx, QWidget *parent)
: QDialog(parent)
{
mCtx = ctx;
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
this->setWindowTitle(tr("Generate Key"));
this->setModal(true);
generateKeyDialog();
}
void KeyGenDialog::generateKeyDialog()
{
errorLabel = new QLabel(tr(""));
nameEdit = new QLineEdit(this);
emailEdit = new QLineEdit(this);
commentEdit = new QLineEdit(this);
keySizeSpinBox = new QSpinBox(this);
keySizeSpinBox->setRange(1024, 4096);
keySizeSpinBox->setValue(2048);
keySizeSpinBox->setSingleStep(1024);
dateEdit = new QDateEdit(QDate::currentDate().addYears(5), this);
dateEdit->setMinimumDate(QDate::currentDate());
dateEdit->setDisplayFormat("dd/MM/yyyy");
dateEdit->setCalendarPopup(true);
dateEdit->setEnabled(true);
expireCheckBox = new QCheckBox(this);
expireCheckBox->setCheckState(Qt::Unchecked);
passwordEdit = new QLineEdit(this);
repeatpwEdit = new QLineEdit(this);
passwordEdit->setEchoMode(QLineEdit::Password);
repeatpwEdit->setEchoMode(QLineEdit::Password);
pwStrengthSlider = new QSlider(this);
pwStrengthSlider->setOrientation(Qt::Horizontal);
pwStrengthSlider->setMaximum(6);
pwStrengthSlider->setDisabled(true);
pwStrengthSlider->setToolTip(tr("Password Strength"));
pwStrengthSlider->setTickPosition(QSlider::TicksBelow);
QGridLayout *vbox1 = new QGridLayout;
vbox1->addWidget(new QLabel(tr("Name:")), 0, 0);
vbox1->addWidget(new QLabel(tr("E-Mailaddress:")), 1, 0);
vbox1->addWidget(new QLabel(tr("Comment:")), 2, 0);
vbox1->addWidget(new QLabel(tr("Expiration Date:")), 3, 0);
vbox1->addWidget(new QLabel(tr("Never Expire")), 3, 3);
vbox1->addWidget(new QLabel(tr("KeySize (in Bit):")), 4, 0);
vbox1->addWidget(new QLabel(tr("Password:")), 5, 0);
vbox1->addWidget(new QLabel(tr("Password: Strength\nWeak -> Strong")), 5, 3);
vbox1->addWidget(new QLabel(tr("Repeat Password:")), 6, 0);
vbox1->addWidget(nameEdit, 0, 1);
vbox1->addWidget(emailEdit, 1, 1);
vbox1->addWidget(commentEdit, 2, 1);
vbox1->addWidget(dateEdit, 3, 1);
vbox1->addWidget(expireCheckBox, 3, 2);
vbox1->addWidget(keySizeSpinBox, 4, 1);
vbox1->addWidget(passwordEdit, 5, 1);
vbox1->addWidget(repeatpwEdit, 6, 1);
vbox1->addWidget(pwStrengthSlider, 6, 3);
QWidget *nameList = new QWidget(this);
nameList->setLayout(vbox1);
QVBoxLayout *vbox2 = new QVBoxLayout();
vbox2->addWidget(nameList);
vbox2->addWidget(errorLabel);
vbox2->addWidget(buttonBox);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(slotKeyGenAccept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
connect(expireCheckBox, SIGNAL(stateChanged(int)), this, SLOT(slotExpireBoxChanged()));
connect(passwordEdit, SIGNAL(textChanged(QString)), this, SLOT(slotPasswordEditChanged()));
this->setLayout(vbox2);
}
void KeyGenDialog::slotKeyGenAccept()
{
QString errorString = "";
QString keyGenParams = "";
/**
* check for errors in keygen dialog input
*/
if ((nameEdit->text()).size() < 5) {
errorString.append(tr(" Name must contain at least five characters. \n"));
}
if (passwordEdit->text() != repeatpwEdit->text()) {
errorString.append(tr(" Password and Repeat don't match. "));
}
if (errorString.isEmpty()) {
/**
* create the string for key generation
*/
keyGenParams = "<GnupgKeyParms format=\"internal\">\n"
"Key-Type: RSA\n"
"Key-Usage: sign\n"
"Key-Length: " + keySizeSpinBox->cleanText() + "\n"
"Subkey-Type: RSA\n"
"Subkey-Length: " + keySizeSpinBox->cleanText() + "\n"
"Subkey-Usage: encrypt\n";
keyGenParams += "Name-Real: " + nameEdit->text().toUtf8() + "\n";
if (!(commentEdit->text().isEmpty())) {
keyGenParams += "Name-Comment: " + commentEdit->text().toUtf8() + "\n";
}
if (!(emailEdit->text().isEmpty())) {
keyGenParams += "Name-Email: " + emailEdit->text().toUtf8() + "\n";
}
if (expireCheckBox->checkState()) {
keyGenParams += "Expire-Date: 0\n";
} else {
keyGenParams += "Expire-Date: " + dateEdit->sectionText(QDateTimeEdit::YearSection) + "-" + dateEdit->sectionText(QDateTimeEdit::MonthSection) + "-" + dateEdit->sectionText(QDateTimeEdit::DaySection) + "\n";
}
if (!(passwordEdit->text().isEmpty())) {
keyGenParams += "Passphrase: " + passwordEdit->text() + "\n";
}
keyGenParams += "</GnupgKeyParms>";
KeyGenThread *kg = new KeyGenThread(keyGenParams, mCtx);
kg->start();
this->accept();
QDialog *dialog = new QDialog(this, Qt::CustomizeWindowHint | Qt::WindowTitleHint);
dialog->setModal(true);
dialog->setWindowTitle(tr("Generating Key..."));
QLabel *waitMessage = new QLabel(tr("Collecting random data for key generation.\n This may take a while.\n To speed up the process use your computer\n (e.g. browse the net, listen to music,...)"));
QProgressBar *pb = new QProgressBar();
pb->setRange(0, 0);
QVBoxLayout *layout = new QVBoxLayout(dialog);
layout->addWidget(waitMessage);
layout->addWidget(pb);
dialog->setLayout(layout);
dialog->show();
while (kg->isRunning()) {
QCoreApplication::processEvents();
}
dialog->close();
QMessageBox::information(0,tr("Success"),tr("New key created"));
} else {
/**
* create error message
*/
errorLabel->setAutoFillBackground(true);
QPalette error = errorLabel->palette();
error.setColor(QPalette::Background, "#ff8080");
errorLabel->setPalette(error);
errorLabel->setText(errorString);
this->show();
}
}
void KeyGenDialog::slotExpireBoxChanged()
{
if (expireCheckBox->checkState()) {
dateEdit->setEnabled(false);
} else {
dateEdit->setEnabled(true);
}
}
void KeyGenDialog::slotPasswordEditChanged()
{
pwStrengthSlider->setValue(checkPassWordStrength());
update();
}
int KeyGenDialog::checkPassWordStrength()
{
int strength = 0;
// increase strength by two, if password has more than 7 characters
if ((passwordEdit->text()).length() > 7) {
strength = strength + 2;
}
// increase strength by one, if password contains a digit
if ((passwordEdit->text()).contains(QRegExp("\\d"))) {
strength++;
}
// increase strength by one, if password contains a lowercase character
if ((passwordEdit->text()).contains(QRegExp("[a-z]"))) {
strength++;
}
// increase strength by one, if password contains an uppercase character
if ((passwordEdit->text()).contains(QRegExp("[A-Z]"))) {
strength++;
}
// increase strength by one, if password contains a non-word character
if ((passwordEdit->text()).contains(QRegExp("\\W"))) {
strength++;
}
return strength;
}
|