-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutil.cpp
48 lines (44 loc) · 1.08 KB
/
util.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
//
// Created by itsuy on 2020/4/1.
//
#include "util.h"
#include <fstream>
#include <random>
#include <base64/base64.h>
using std::ifstream;
int Util::readJsonConfig(const char *filename, DynamicJsonDocument &doc) {
ifstream infile;
infile.open(filename);
if (!infile.is_open()) {
return 0;
}
deserializeJson(doc, infile);
infile.close();
return 1;
}
std::string Util::genPassword() {
std::random_device rd; // 将用于为随机数引擎获得种子
std::mt19937 gen(rd()); // 以播种标准 mersenne_twister_engine
std::uniform_int_distribution<> dis(0, 255); //0-255
unsigned char pwd[256] = {};
int index = 0;
while (true) {
bool flag = true;
auto num = dis(gen);
for (auto j = 0; j < index; j++) {
if (num == pwd[j]) {
flag = false;
break;
}
}
if (flag) {
pwd[index] = num;
index++;
}
if (index == 256) {
break;
}
}
std::string p = base64_encode(pwd, 256);
return p;
}