-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpasswordgenerator.cpp
48 lines (44 loc) · 1.06 KB
/
passwordgenerator.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
extern "C" {
#include "sodium.h"
}
#include "passwordgenerator.h"
PasswordGenerator::PasswordGenerator(QObject *parent) : QObject(parent)
{
if (sodium_init() < 0) {
sodiumFailed = true;
} else {
sodiumFailed = false;
}
}
void PasswordGenerator::generatePassword()
{
if (sodiumFailed && sodium_init()<0) {
emit passwordChanged(Password());
return;
}
const std::pair<QString,QString> *list;
if (codeType == wubi) {
list = wubiWordList;
} else if (codeType == pinyin) {
list = pinyinWordList;
}
password.resize(length);
for (auto iter = password.begin(); iter != password.end(); ++iter) {
*iter = list[randombytes_uniform(8192)];
}
emit passwordChanged(password);
}
void PasswordGenerator::setLength(int length)
{
if (this->length == length)
return;
this->length = length;
generatePassword();
}
void PasswordGenerator::setType(CodeType codeType)
{
if (this->codeType == codeType)
return;
this->codeType = codeType;
generatePassword();
}