-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasswordUtils.cs
226 lines (193 loc) · 6.87 KB
/
PasswordUtils.cs
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
using System;
using System.Collections.Generic;
namespace PasswUtils
{
public enum PasswordWeakLevel
{
NotDefined,
VeryWeak,
Weak,
Sufficient,
Strength,
Resistant,
VeryResistant
}
class PasswordUtils
{
private static int MaxRepeatsChar = 2;
public static string GenerateStrongPass(int PassLength = 7, bool UsePunctuation = true, bool UseEntropy = true)
{
Random randC = new Random();
string result = char.ToString((char)randC.Next(65, 90));
List<char[]> Intrvs = new List<char[]>();
char[] charr;
if (UsePunctuation)
{
string specChars = @"!#$%&*+-/=?@\^_|~"; //@"!#$%&()*+-/;<=>?@[\]^_{|}~";
charr = specChars.ToCharArray();
Intrvs.Add(charr);
}
string gl = "AaEeIiOoUu";
charr = gl.ToCharArray();
Intrvs.Add(charr);
string sl = "BbCcDdFfGgHhJjKkLMmNnPpQqRrSsTtVvWwXxYyZz";
charr = sl.ToCharArray();
Intrvs.Add(charr);
string dl = "189647235";
charr = dl.ToCharArray();
Intrvs.Add(charr);
Random randI = new Random();
while (result.Length < PassLength)
{
int rndIndex = randI.Next(Intrvs.Count);
int charIndex = randC.Next(Intrvs[rndIndex].Length);
string C = char.ToString(Intrvs[rndIndex][charIndex]);
if (!result.Contains(C) && (result.Length < 35))
{
result += C;
}
else
if (!UseEntropy)
result += C;
}
return result;
}
public static PasswordWeakLevel TestPasswordWeak(string password, string login)
{
var _res = PasswordWeakLevel.NotDefined;
if (!IsPasswordLikeLogin(password, login) && !IsQwertyPasswords(password))
{
if (password.Length > 5)
{
if (ContainsUpperLetter(password) && ContainsLowerLetter(password)) _res = PasswordWeakLevel.Weak;
if (ContainsDigit(password)) _res++; //Sufficient
if (RepeatSigns(password)) _res--;
if (IsStriping(password)) _res++; //Strength
if (ContainsPunctuation(password) && ContainsSeparator(password)) _res++; //Resistant
if ((_res == PasswordWeakLevel.Resistant) && (password.Length > 15)) _res++;
}
else
_res = PasswordWeakLevel.VeryWeak;
}
return _res;
}
private static bool IsQwertyPasswords(string pass)
{
var keyboardlist = new List<string>();
keyboardlist.Add("azer");
keyboardlist.Add("qwer");
keyboardlist.Add("asdf");
keyboardlist.Add("zxcv");
keyboardlist.Add("1234");
foreach (string item in keyboardlist)
{
if (pass.ToLower().Trim().Contains(item.ToLower()))
return true;
}
return false;
}
private static bool IsPasswordLikeLogin(string pass, string login)
{
if (login != string.Empty)
{
if (login.ToLower().Trim().Contains(pass.ToLower().Trim()) || pass.ToLower().Trim().Contains(login.ToLower().Trim()))
{
return true;
}
string reversed = "";
char[] passarr = pass.ToCharArray();
for (int i = pass.Length - 1; i >= 0; i--)
{
reversed = reversed + passarr[i];
}
if (reversed.ToLower().Trim().Contains(login.ToLower().Trim()))
{
return true;
}
}
return false;
}
private static bool RepeatSigns(string pass)
{
char lastchar = (char)0;
int _rep = 0;
foreach (char c in pass)
{
if (lastchar == c)
{
_rep++;
if (_rep > MaxRepeatsChar)
return true;
}
lastchar = c;
}
return false;
}
private static bool IsStriping(string pass) // фактор перемеживание букв и символов
{
int stripcount = 0;
char lastchar = (char)0;
foreach (char c in pass)
{
if ((Char.IsLetterOrDigit(c) && ((Char.IsLetter(c) && Char.IsDigit(lastchar)) || (Char.IsDigit(c) && Char.IsLetter(lastchar)))) ||
((Char.IsLetterOrDigit(c) && !Char.IsLetterOrDigit(lastchar)) || ((Char.IsLetterOrDigit(lastchar) && !Char.IsLetterOrDigit(c)))))
{
stripcount++;
if (stripcount > pass.Length / 3)
return true;
}
lastchar = c;
}
return false;
}
private static bool ContainsLowerLetter(string pass)
{
foreach (char c in pass)
{
if ((Char.IsLetter(c)) && (Char.IsLower(c)))
return true;
}
return false;
}
private static bool ContainsUpperLetter(string pass)
{
foreach (char c in pass)
{
if ((Char.IsLetter(c)) && (Char.IsUpper(c)))
return true;
}
return false;
}
private static bool ContainsDigit(string pass)
{
foreach (char c in pass)
{
if (Char.IsDigit(c))
return true;
}
return false;
}
private static bool ContainsPunctuation(string pass)
{
foreach (char c in pass)
{
if (Char.IsPunctuation(c))
return true;
}
return false;
}
private static bool ContainsSeparator(string pass)
{
foreach (char c in pass)
{
if (Char.IsSeparator(c))
return true;
}
return false;
}
public static bool BadPasswordSecurity(string pass, string Login)
{
return ((int)PasswordUtils.TestPasswordWeak(pass, Login) < 5);
}
}
}