-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotpdialog.cpp
239 lines (179 loc) · 6.13 KB
/
hotpdialog.cpp
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
239
/*
* Author: Copyright (C) Andrzej Surowiec 2012
*
*
* This file is part of GPF Crypto Stick.
*
* GPF Crypto Stick 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
* any later version.
*
* GPF Crypto Stick 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 GPF Crypto Stick. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QDateTime>
#include <QDebug>
#include <QClipboard>
#include "hotpdialog.h"
#include "ui_hotpdialog.h"
HOTPDialog::HOTPDialog( QWidget *parent) :
QDialog(parent),
ui(new Ui::HOTPDialog)
{
TOTP_ValidTimer = new QTimer(this);
// Start timer for polling stick response
connect(TOTP_ValidTimer, SIGNAL(timeout()), this, SLOT(checkTOTP_Valid()));
TOTP_ValidTimer->start(100);
Clipboard_ValidTimer = new QTimer(this);
// Start timer for polling stick response
connect(Clipboard_ValidTimer, SIGNAL(timeout()), this, SLOT(checkClipboard_Valid()));
Clipboard_ValidTimer->start(100);
clipboard = QApplication::clipboard();
ui->setupUi(this);
}
void HOTPDialog::getNextCode()
{
uint8_t result[18];
memset(result,0,18);
uint32_t code;
uint8_t config;
lastInterval = ui->intervalSpinBox->value();
if (lastInterval<1)
lastInterval=1;
if (slotNumber>=0x20)
device->TOTPSlots[slotNumber-0x20]->interval = lastInterval;
QString output;
lastTOTPTime = QDateTime::currentDateTime().toTime_t();
device->getCode(slotNumber,lastTOTPTime/lastInterval,lastTOTPTime,lastInterval,result);
//cryptostick->getCode(slotNo,1,result);
code=result[0]+(result[1]<<8)+(result[2]<<16)+(result[3]<<24);
config=result[4];
/*if (config&(1<<2))
output.append(QByteArray((char *)(result+5),12));*/
if (config&(1<<0)){
code=code%100000000;
output.append(QString( "%1" ).arg(QString::number(code),8,'0') );
}
else{
code=code%1000000;
output.append(QString( "%1" ).arg(QString::number(code),6,'0') );
}
qDebug() << "Current time:" << lastTOTPTime;
qDebug() << "Counter:" << lastTOTPTime/lastInterval;
qDebug() << "TOTP:" << code;
ui->lineEdit->setText(output);
copyToClipboard(ui->lineEdit->text());
}
void HOTPDialog::setToHOTP()
{
ui->label->setText("Your HOTP:");
ui->nextButton->setText("Next HOTP");
ui->labelNotify->setText("HOTP copied to clipboard");
this->setWindowTitle(title);
ui->intervalLabel->hide();
ui->intervalSpinBox->hide();
ui->validTimer->hide();
}
void HOTPDialog::setToTOTP()
{
ui->label->setText("Your TOTP:");
ui->nextButton->setText("Generate TOTP");
this->setWindowTitle(title);
ui->intervalLabel->show();
ui->labelNotify->setText("TOTP copied to clipboard");
ui->intervalSpinBox->setValue(device->TOTPSlots[slotNumber-0x20]->interval);
ui->intervalSpinBox->show();
ui->validTimer->show();
}
HOTPDialog::~HOTPDialog()
{
// Kill timer
TOTP_ValidTimer->stop();
delete TOTP_ValidTimer;
delete ui;
}
void HOTPDialog::on_nextButton_clicked()
{
getNextCode();
}
void HOTPDialog::copyToClipboard(QString text)
{
lastClipboardTime = QDateTime::currentDateTime().toTime_t();
clipboard->setText(text);
ui->labelNotify->show();
// this->accept();
}
/*******************************************************************************
checkTOTP_Valid
Changes
Date Author Info
17.03.14 RB Function created
Reviews
Date Reviewer Info
*******************************************************************************/
#define TOTP_MORE_THAN_5_SEC_TO_INVALID 0
#define TOTP_LESS_THAN_5_SEC_TO_INVALID 1
#define TOTP_IS_INVALID 2
void HOTPDialog::checkTOTP_Valid()
{
uint64_t currentTime;
uint64_t checkTime;
uint8_t state;
QPalette palette;
state = TOTP_IS_INVALID;
currentTime = QDateTime::currentDateTime().toTime_t();
checkTime = (lastTOTPTime/(uint64_t)lastInterval)*(uint64_t)lastInterval;
if (checkTime + (uint64_t)lastInterval - (uint64_t)5 > currentTime)
{
state = TOTP_MORE_THAN_5_SEC_TO_INVALID;
}
if ((checkTime + (uint64_t)lastInterval - (uint64_t)5 <= currentTime) && (checkTime + lastInterval > currentTime))
{
state = TOTP_LESS_THAN_5_SEC_TO_INVALID;
}
palette = ui->validTimer->palette();
ui->validTimer->setAutoFillBackground(true);
switch (state)
{
case TOTP_MORE_THAN_5_SEC_TO_INVALID :
ui->validTimer->setText("Valid");
palette.setColor(ui->validTimer->backgroundRole(), Qt::green);
break;
case TOTP_LESS_THAN_5_SEC_TO_INVALID :
ui->validTimer->setText("Valid");
palette.setColor(ui->validTimer->backgroundRole(), Qt::yellow);
break;
case TOTP_IS_INVALID :
ui->validTimer->setText("Invalid");
palette.setColor(ui->validTimer->backgroundRole(), Qt::red);
break;
}
ui->validTimer->setPalette(palette);
}
/*******************************************************************************
checkClipboard_Valid
Changes
Date Author Info
12.07.14 SN Function created
Reviews
Date Reviewer Info
*******************************************************************************/
void HOTPDialog::checkClipboard_Valid()
{
uint64_t currentTime;
//uint64_t checkTime;
currentTime = QDateTime::currentDateTime().toTime_t();
if(currentTime >= lastClipboardTime + (uint64_t)60 && (QString::compare(clipboard->text(),ui->lineEdit->text())==0)){
copyToClipboard(QString(""));
ui->labelNotify->hide();
}
if (QString::compare(clipboard->text(),ui->lineEdit->text())!=0){
ui->labelNotify->hide();
}
}