-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.c
79 lines (68 loc) · 2.4 KB
/
lib.c
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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#include <time.h>
enum Rotation {
LowerAlpha,
UpperAlpha,
Numbers,
SpecialBeta,
SpecialMid,
SpecialBased,
};
char* generatePassword(int* len, int* difficultyLevel) {
char* password = (char*) malloc(sizeof(char) * (unsigned long) *len);
char* alphabets = "abcdefghijklmnopqrstuvwxyz";
int alphLen = strlen(alphabets);
char* capitalizedAlphabets = malloc(alphLen);
if (capitalizedAlphabets == NULL) {
printf("Failed to allocate memory for capitalizedAlphabets");
exit(1);
}
for (int i = 0; i < alphLen; i++) {
capitalizedAlphabets[i] = toupper(alphabets[i]);
}
capitalizedAlphabets[alphLen] = '\0';
char* numbers = "0123456789";
char* specialCharactersLevel0 = "()-+=";
char* specialCharactersLevel1 = "!@#$";
char* specialCharactersLevel2 = "%^&*;:/\\?\"'[]";
for (int i = 0; i < *len; i++) {
srand(time(NULL) + i);
enum Rotation rotationptr = rand() % (3 + *difficultyLevel);
switch (rotationptr) {
case 0:
srand(time(NULL) + i + rotationptr);
password[i] = alphabets[rand() % alphLen];
break;
case 1:
srand(time(NULL) + i + rotationptr);
password[i] = capitalizedAlphabets[rand() % alphLen];
break;
case 2:
srand(time(NULL) + i + rotationptr);
password[i] = numbers[rand() % (strlen(numbers) - 1)];
break;
case 3:
srand(time(NULL) + i + rotationptr);
password[i] = specialCharactersLevel0[rand() % (strlen(specialCharactersLevel0) - 1)];
break;
case 4:
srand(time(NULL) + i + rotationptr);
password[i] = specialCharactersLevel1[rand() % (strlen(specialCharactersLevel1) - 1)];
break;
case 5:
srand(time(NULL) + i + rotationptr);
password[i] = specialCharactersLevel2[rand() % (strlen(specialCharactersLevel2) - 1)];
break;
default:
srand(time(NULL) + i + rotationptr);
password[i] = alphabets[rand() % alphLen];
break;
}
}
free(capitalizedAlphabets);
return password;
}