-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathotpwidget.cpp
More file actions
66 lines (53 loc) · 1.28 KB
/
otpwidget.cpp
File metadata and controls
66 lines (53 loc) · 1.28 KB
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
#include "otpwidget.h"
#include "ui_otpwidget.h"
#include <ctime>
#include <oath.h>
#include <QDebug>
OTPWidget::OTPWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::OTPWidget)
{
ui->setupUi(this);
}
OTPWidget::~OTPWidget()
{
delete ui;
}
void OTPWidget::setService(QString service)
{
ui->serviceNameLabel->setText(service);
}
void OTPWidget::setSecret(QString secret)
{
m_secret=secret;
}
QString OTPWidget::totpGenerate()
{
QByteArray secret = m_secret.toUtf8();
char **secretDecoded;
size_t *secretLength;
qDebug() << m_secret << m_secret.size();
if (oath_base32_decode(secret.data(), m_secret.size(), secretDecoded, secretLength) != OATH_OK) {
ui->codeLabel->setText("ERROR");
return QString();
}
char totpCode[16];
if (oath_totp_generate(*secretDecoded, *secretLength, std::time(0), 0, 0, 6, totpCode) == OATH_OK)
{
ui->codeLabel->setText(totpCode);
return totpCode;
}
ui->codeLabel->setText("ERROR");
return QString();
}
void OTPWidget::updateWidget()
{
int epoch = std::time(0);
int countDown = 30 - (epoch % 30);
if (epoch % 30 == 29) {
totpGenerate();
}
ui->timerBar->setValue(countDown);
qDebug() << QString::number(countDown);
//totpGenerate();
}