-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrandnum.cpp
50 lines (41 loc) · 1.24 KB
/
randnum.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
#include "randnum.h"
#include "ui_randnum.h"
RandNum::RandNum(QWidget *parent)
: QWidget(parent)
, ui(new Ui::RandNum)
{
ui->setupUi(this);
/* 限制只能输入整数且范围为[1,128 * 1024]*/
QIntValidator *aIntValidator = new QIntValidator;
aIntValidator->setRange(1, 131072);
ui->lineEditInput->setValidator(aIntValidator);
}
RandNum::~RandNum()
{
delete ui;
}
void RandNum::on_pushButtonGen_clicked()
{
QString inputByte = this->ui->lineEditInput->text();
int randNumByte = inputByte.toInt();
size_t len = randNumByte * 2 + 1;
std::vector<unsigned char> buf;
std::vector<char> str;
buf.reserve(randNumByte);
int ret = RAND_bytes((unsigned char *) buf.data(), randNumByte);
if (ret == 0) {
printTSError();
} else {
str.reserve(len);
if (OPENSSL_buf2hexstr_ex(str.data(),
len,
NULL,
(unsigned char *) buf.data(),
randNumByte,
'\0')
!= 1)
return;
this->ui->textBrowserOutput->setText(QString::fromStdString(std::string(str.data(), len)));
}
return;
}