-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_generator.cpp
More file actions
163 lines (134 loc) · 4.67 KB
/
Copy pathpassword_generator.cpp
File metadata and controls
163 lines (134 loc) · 4.67 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
#include <bits/stdc++.h>
using namespace std;
int selectArray()
{
srand(time(nullptr));
int i = rand() % 5;
if (i == 0.0)
i++;
return i;
}
int getKey()
{
srand(time(nullptr));
int key = rand() % 26;
return key;
}
void generate_password(int length)
{
string password;
string alphabet = "abcdefghijklmnopqrstuvwxyz";
string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string s_symbol = "!@#$%&";
string number = "0123456789";
int key;
int count_alphabet = 0;
int count_ALPHABET = 0;
int count_number = 0;
int count_s_symbol = 0;
int count = 0;
while (count < length) {
int k = selectArray();
if (count == 0) {
k = k % 3;
if (k == 0)
k++;
}
switch (k) {
case 1:
// following if condition will check if minimum requirement of alphabet
// character has been fulfilled or not
// in case it has been fulfilled and minimum requirements of other
// characters is still left then it will break ;
if ((count_alphabet == 2) && (count_number == 0 || count_ALPHABET == 0 || count_ALPHABET == 1 || count_s_symbol == 0))
break;
key = getKey();
password = password + alphabet[key];
count_alphabet++;
count++;
break;
case 2:
// following if condition will check if minimum requirement of
// ALPHABET character has been fulfilled or not
// in case it has been fulfilled and minimum requirements of
// other characters is still left then it will break ;
if ((count_ALPHABET == 2) && (count_number == 0 || count_alphabet == 0 || count_alphabet == 1 || count_s_symbol == 0))
break;
key = getKey();
password = password + ALPHABET[key];
count_ALPHABET++;
count++;
break;
case 3:
// following if condition will check if minimum requirement
// of Numbers has been fulfilled or not
// in case it has been fulfilled and minimum requirements of
// other characters is still left then it will break ;
if ((count_number == 1) && (count_alphabet == 0 || count_alphabet == 1 || count_ALPHABET == 1 || count_ALPHABET == 0 || count_s_symbol == 0))
break;
key = getKey();
key = key % 10;
password = password + number[key];
count_number++;
count++;
break;
case 4:
// following if condition will check if minimum requirement of
// Special symbol character has been fulfilled or not
// in case it has been fulfilled and minimum requirements of
// other characters is still left then it will break ;
if ((count_s_symbol == 1) && (count_alphabet == 0 || count_alphabet == 1 || count_ALPHABET == 0 || count_ALPHABET == 1 || count_number == 0))
break;
key = getKey();
key = key % 6;
password = password + s_symbol[key];
count_s_symbol++;
count++;
break;
}
}
cout << "\n\n";
cout << "Password : "<< password;;
cout << "\n\n";
}
int main()
{
int opt, length;
//Menu
do {
cout << "\n\n";
cout << " Random Password Generator\n";
cout << "\n";
cout << " 1. Generate Password"
<< "\n";
cout << " 2. Exit"
<< "\n\n";
cout << "> ";
cin >> opt;
switch (opt) {
case 1:
cout << "\n\nEnter Length (greater than 7): ";
cin >> length;
//if length is less than 7 , program will show error
if (length < 7) {
cout << "\nError : Password Length Should be atleast 7\n";
}
// Length should not exceed 100 , program should show error if it exceeds
else if (length > 100) {
cout << "\nError : Maximum length of password should be 100\n";
}
//Otherwise call generate_password() function to generate password
else
generate_password(length);
break;
default:
// If invalid option is chosen by user it will also show error
if (opt != 2) {
printf("\nInvalid choice\n");
printf("Please Press ( 1 ) to generate password and ( 2 ) to exit.\n");
}
break;
}
} while (opt != 2);
return 0;
}