-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
leccore.cpp
309 lines (250 loc) · 7.54 KB
/
leccore.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//
// leccore.h - leccore implementation
//
// leccore library, part of the liblec library
// Copyright (c) 2019 Alec Musasa (alecmus at live dot com)
//
// Released under the MIT license. For full details see the
// file LICENSE.txt
//
#include "leccore.h"
#include "versioninfo.h"
#include <Windows.h>
#include <strsafe.h> // for StringCchPrintfA
#include <thread>
#include <chrono>
#include <sstream>
#include <unordered_map>
// crypto++
#ifdef _WIN64
#ifdef _DEBUG
#pragma comment(lib, "cryptlib64d.lib")
#else
#pragma comment(lib, "cryptlib64.lib")
#endif
#else
#ifdef _DEBUG
#pragma comment(lib, "cryptlib32d.lib")
#else
#pragma comment(lib, "cryptlib32.lib")
#endif
#endif
// DllMain function
BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved) {
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
default:
break;
}
return TRUE;
}
std::string liblec::leccore::version() {
return leccorename + std::string(" ") + leccoreversion + std::string(", ") + leccoredate;
}
namespace nmReadableSize {
/*
** adapted from wxWidgets whose license is as follows:
**
** Name: src / common / filename.cpp
** Purpose: wxFileName - encapsulates a file path
** Author: Robert Roebling, Vadim Zeitlin
** Modified by:
** Created: 28.12.2000
** RCS-ID: $Id$
** Copyright: (c) 2000 Robert Roebling
** Licence: wxWindows licence
*/
// size conventions
enum SizeConvention {
SIZE_CONV_TRADITIONAL, // 1024 bytes = 1KB
SIZE_CONV_SI // 1000 bytes = 1KB
};
std::string GetHumanReadableSize(const long double& dSize,
const std::string& nullsize,
int precision,
SizeConvention conv) {
// deal with trivial case first
if (dSize == 0)
return nullsize;
// depending on the convention used the multiplier may be either 1000 or
// 1024 and the binary infix may be empty (for "KB") or "i" (for "KiB")
long double multiplier = 1024.;
switch (conv) {
case SIZE_CONV_TRADITIONAL:
// nothing to do, this corresponds to the default values of both
// the multiplier and infix string
break;
case SIZE_CONV_SI:
multiplier = 1000;
break;
}
const long double kiloByteSize = multiplier;
const long double megaByteSize = multiplier * kiloByteSize;
const long double gigaByteSize = multiplier * megaByteSize;
const long double teraByteSize = multiplier * gigaByteSize;
const long double bytesize = dSize;
size_t const cchDest = 256;
char pszDest[cchDest];
if (bytesize < kiloByteSize)
StringCchPrintfA(pszDest, cchDest, "%.*fB", 0, dSize);
else if (bytesize < megaByteSize)
StringCchPrintfA(pszDest, cchDest, "%.*fKB", precision, bytesize / kiloByteSize);
else if (bytesize < gigaByteSize)
StringCchPrintfA(pszDest, cchDest, "%.*fMB", precision, bytesize / megaByteSize);
else if (bytesize < teraByteSize)
StringCchPrintfA(pszDest, cchDest, "%.*fGB", precision, bytesize / gigaByteSize);
else
StringCchPrintfA(pszDest, cchDest, "%.*fTB", precision, bytesize / teraByteSize);
return pszDest;
}
}
std::string liblec::leccore::format_size(unsigned long long size, unsigned short precision) {
return nmReadableSize::GetHumanReadableSize(static_cast<long double>(size), "0B", precision,
nmReadableSize::SIZE_CONV_TRADITIONAL);
}
void liblec::leccore::sleep(unsigned long long milliseconds) {
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
}
liblec::leccore::password_quality_specs liblec::leccore::password_quality(const std::string& password) {
// returns the number of duplicates in a given string
auto number_of_duplicates = [](const std::string& str) {
// K = character, T = occurences
std::unordered_map<char, int> pairs;
for (const auto& character : str)
pairs[character]++;
int duplicates = 0;
for (const auto& [character, occurences] : pairs)
duplicates += (occurences - 1);
return duplicates;
};
password_quality_specs quality;
quality.strength = 0.f;
if (password.empty()) {
quality.issues.push_back("No password");
quality.issues_summary = "No password";
return quality;
}
// number of items of each type
float hits_lower = 0;
float hits_upper = 0;
float hits_digit = 0;
float hits_special = 0;
for (const auto& character : password) {
if (islower(character))
hits_lower++;
else
if (isupper(character))
hits_upper++;
else
if (isdigit(character))
hits_digit++;
else
hits_special++;
}
// check for duplicate characters
int duplicates = number_of_duplicates(password);
// add strength at different proportions for different elements
quality.strength = quality.strength +
hits_lower * 3.f +
hits_upper * 3.f +
hits_digit * 2.f + // digits have the least weight
hits_special * 5.f; // special characters have the greatest weight
// add strength according to complexity of mixing
const float mix_factor =
(hits_lower * hits_upper) +
(hits_lower * hits_digit) +
(hits_lower * hits_special) +
(hits_upper * hits_digit) +
(hits_upper * hits_special) +
(hits_digit * hits_special);
quality.strength = quality.strength + mix_factor - duplicates * 2.5f;
// impose limits
quality.strength = max(quality.strength, 0.f);
quality.strength = min(quality.strength, 100.f);
// issues
if (hits_lower < 3) {
if (hits_lower == 0)
quality.issues.push_back("No lowercase characters");
else
quality.issues.push_back("Few lowercase characters");
}
if (hits_upper < 3) {
if (hits_upper == 0)
quality.issues.push_back("No uppercase characters");
else
quality.issues.push_back("Few uppercase characters");
}
if (hits_special < 3) {
if (hits_special == 0)
quality.issues.push_back("No special characters");
else
quality.issues.push_back("Few special characters");
}
if (hits_digit < 3) {
if (hits_digit == 0)
quality.issues.push_back("No digits");
else
quality.issues.push_back("Few digits");
}
if (duplicates > 2)
quality.issues.push_back("Duplicate characters");
// issues summary
for (auto issue : quality.issues) {
if (quality.issues_summary.empty())
quality.issues_summary = issue;
else {
if (!issue.empty())
issue[0] = tolower(issue[0]);
quality.issues_summary += ", " + issue;
}
}
return quality;
}
std::string liblec::leccore::round_off::to_string(const double& d, int precision) {
std::stringstream ss;
ss << std::fixed;
ss.precision(precision);
ss << d;
return ss.str();
}
double liblec::leccore::round_off::to_double(const double& d, int precision) {
int y = (int)d;
double z = d - (double)y;
double m = pow(10.0, (double)precision);
double q = z * m;
double r = round(q);
return static_cast<double>(y) + (1.0 / m) * r;
}
float liblec::leccore::round_off::to_float(const float& f, int precision) {
int y = (int)f;
float z = f - (float)y;
float m = pow(10.f, (float)precision);
float q = z * m;
float r = round(q);
return static_cast<float>(y) + (1.f / m) * r;
}
liblec::leccore::size::size() :
size(0.f, 0.f) {}
liblec::leccore::size::size(const float width, const float height) :
_width(width), _height(height) {}
float& liblec::leccore::size::width() { return _width; }
float liblec::leccore::size::get_width() const { return _width; }
liblec::leccore::size& liblec::leccore::size::width(const float& width) {
_width = width;
return *this;
}
float& liblec::leccore::size::height() { return _height; }
float liblec::leccore::size::get_height() const { return _height; }
liblec::leccore::size& liblec::leccore::size::height(const float& height) {
_height = height;
return *this;
}