-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhxPasswordGenerator.c
More file actions
299 lines (271 loc) · 5.39 KB
/
hxPasswordGenerator.c
File metadata and controls
299 lines (271 loc) · 5.39 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
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
/*
* 22.09.2016
* Консольный генератор паролей.
* github.com/TyUser/hxPasswordGenerator
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
void HXpassword(char *sPassword, unsigned int iQuality, unsigned int iType)
{
FILE *fileTXT;
fileTXT = fopen("password.txt", "a");
if (fileTXT != NULL)
{
char sTime[32];
time_t t = time(NULL);
struct tm *aTm = localtime(&t);
strftime(sTime, sizeof(sTime)-1, "%H:%M:%S %d_%m_%Y", aTm);
if (iType == 3)
{
fprintf(fileTXT, "%s PIN code: %s\n", sTime, sPassword);
}
else
{
fprintf(fileTXT, "%s Password %d: %s\n", sTime, iQuality, sPassword);
}
fclose(fileTXT);
}
}
void HXrand(const char *sBuf, unsigned int iQuality, unsigned int iType, unsigned int iNumber)
{
char sBuf1[32] = {0};
unsigned int iBuf1[12000] = {0};
unsigned int iLen;
unsigned int iRand1;
unsigned int iRand2;
unsigned int i;
iLen = (unsigned int)strlen(sBuf);
if (iLen > 1)
{
iRand1 = 0;
iRand2 = 0;
i = 0;
iNumber += 1000;
if (iNumber > 11000)
{
iNumber = 11000;
}
while (i < iNumber)
{
iBuf1[i] = (unsigned int)rand() % iLen;
i += 1;
}
i = 0;
while (i < iQuality)
{
iRand1 = (unsigned int)rand() % (iNumber - 1);
iRand2 = iBuf1[iRand1];
sBuf1[i] = sBuf[iRand2];
i += 1;
}
sBuf1[i] = '\0';
if (iType == 3)
{
printf("PIN code: %s\n", sBuf1);
}
else
{
printf("Password: %s\n", sBuf1);
}
HXpassword(sBuf1, iQuality, iType);
}
}
void HXfileW(unsigned int iQuality, unsigned int iType, unsigned int iNumber)
{
FILE *fileW;
fileW = fopen("settings.ini", "w+");
if (fileW)
{
fprintf(fileW, "[Options]\n");
fprintf(fileW, "# v 2.0\n");
fprintf(fileW, "# github.com/TyUser/hxPasswordGenerator\n\n");
fprintf(fileW, "# качество пароля\n");
fprintf(fileW, "# от 12 до 20\n");
fprintf(fileW, "quality = %d\n\n", iQuality);
fprintf(fileW, "# тип пароля\n");
fprintf(fileW, "# от 1 до 4\n");
fprintf(fileW, "type = %d\n\n", iType);
fprintf(fileW, "# для индивидуального\n");
fprintf(fileW, "# рандома\n");
fprintf(fileW, "# от 1 до 10000\n");
fprintf(fileW, "number = %d\n\n", iNumber);
fclose(fileW);
}
}
void HXtype(unsigned int iQuality, unsigned int iType, unsigned int iNumber)
{
switch(iType)
{
case 1:
{
HXrand("1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz", iQuality, iType, iNumber);
break;
}
case 2:
{
HXrand("123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnPpQqRrSsTtUuVvWwXxYyZz", iQuality, iType, iNumber);
break;
}
case 3:
{
HXrand("1234567890", 3, iType, iNumber);
break;
}
case 4:
{
HXrand("1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz!#$%&()*+,-:;<=>?_.", iQuality, iType, iNumber);
}
}
}
void HXfilter(char *sOld, char *sNew)
{
unsigned int iBuf1 = 0;
unsigned int iBuf2 = 0;
while (sOld[iBuf1] != '\0')
{
if ((sOld[iBuf1] >= '0') && (sOld[iBuf1] <= '9'))
{
sNew[iBuf2] = sOld[iBuf1];
iBuf2 += 1;
}
if ((sOld[iBuf1] >= 'a') && (sOld[iBuf1] <= 'z'))
{
sNew[iBuf2] = sOld[iBuf1];
iBuf2 += 1;
}
if ((sOld[iBuf1] >= 'A') && (sOld[iBuf1] <= 'Z'))
{
sNew[iBuf2] = sOld[iBuf1];
iBuf2 += 1;
}
if ((sOld[iBuf1] == '=') || (sOld[iBuf1] == '[') || (sOld[iBuf1] == ']'))
{
sNew[iBuf2] = sOld[iBuf1];
iBuf2 += 1;
}
if ((sOld[iBuf1] == '#') || (sOld[iBuf1] == ';') || (sOld[iBuf1] == '/'))
{
break;
}
iBuf1 += 1;
}
sNew[iBuf2] = '\0';
}
void HXvalidate(unsigned int iQuality, unsigned int iType, unsigned int iNumber)
{
unsigned int iRand;
unsigned int iError;
srand((unsigned int)time(NULL));
iRand = 1 + (unsigned int)rand() % 10000;
iError = 0;
if (iQuality < 12)
{
iQuality = 16;
iError = 1;
}
if (iQuality > 20)
{
iQuality = 20;
iError = 1;
}
if (iType < 1)
{
iType = 2;
iError = 1;
}
if (iType > 4)
{
iType = 4;
iError = 1;
}
if (iNumber < 1)
{
iNumber = iRand;
iError = 1;
}
if (iNumber > 10000)
{
iNumber = iRand;
iError = 1;
}
if (iError)
{
HXfileW(iQuality, iType, iNumber);
}
HXtype(iQuality, iType, iNumber);
}
void HXfileR(void)
{
FILE *fileR;
char sBuf1[1024] = {0};
char sBuf2[1024] = {0};
char sKey1[16] = {0};
char sKey2[8] = {0};
unsigned int iQuality = 0;
unsigned int iNumber = 0;
unsigned int iType = 0;
unsigned int i = 0;
fileR = fopen("settings.ini", "r");
if (fileR)
{
while (i <= 17)
{
sBuf1[0] = '\0';
sBuf2[0] = '\0';
sKey1[0] = '\0';
sKey2[0] = '\0';
if (!fgets(sBuf1, sizeof(sBuf1)-10, fileR))
{
break;
}
HXfilter(sBuf1, sBuf2);
if (sscanf(sBuf2, "%8[^=]=%4[0-9]", sKey1, sKey2) == 2)
{
if (!strcmp(sKey1, "quality"))
{
iQuality = (unsigned int)atoi(sKey2);
}
if (!strcmp(sKey1, "type"))
{
iType = (unsigned int)atoi(sKey2);
}
if (!strcmp(sKey1, "number"))
{
iNumber = (unsigned int)atoi(sKey2);
}
}
i += 1;
}
fclose(fileR);
}
HXvalidate(iQuality, iType, iNumber);
}
int main(int argc, char * argv[])
{
unsigned int iQuality = 0;
unsigned int iNumber = 0;
unsigned int iType = 0;
if (argc > 1)
{
if (argv[1] != NULL)
{
iQuality = (unsigned int)atoi(argv[1]);
}
if (argv[2] != NULL)
{
iType = (unsigned int)atoi(argv[2]);
}
if (argv[3] != NULL)
{
iNumber = (unsigned int)atoi(argv[3]);
}
HXvalidate(iQuality, iType, iNumber);
}
else
{
HXfileR();
}
return 0;
}