/
PasswordGenerator.cs
243 lines (212 loc) · 16.4 KB
/
PasswordGenerator.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System;
using System.Security.Cryptography;
using System.Data;
using System.Configuration;
using System.Web;
using System.Messaging;
using System.Collections;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
using System.Text;
using System.Data.SqlClient;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Data.OleDb;
using System.Data.SqlTypes;
using System.IO;
using System.Threading;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Collections.Generic;
namespace MHP20ClassLib {
public class PasswordGenerator {
/*
*
* ومن فرعها في دبي، تقدم M: حالياً خدماتها لعملائها في المملكة العربية السعودية ومصر والعراق وأبوظبي والشارقة ورأس الخيمة. كما قدمت الشركة خدماتها خلال السنوات الأخيرة في الأراضي الفلسطينية وسوريا وعُمان. ولتعزيز ورفع مستوى عروض خدماتها الأساسية، تتمتع الشركة بعلاقات متميزة مع عدد من الشركاء المحليين في أنحاء منطقة الخليج، وبالتحديد في كل من البحرين وقطر والمملكة العربية السعودية ومصر والكويت.
* */
public PasswordGenerator() {
}
public string CreatePassword(){
Password password = new Password(false, true, true, true);
password.MinimumLength = 8;
password.MaximumLength = 8;
return password.Create();
}
public string CreatePassword(int minlen, int maxlen) {
Password password = new Password(false, true, true, true);
password.MinimumLength = minlen;
password.MaximumLength = maxlen;
return password.Create();
}
}
public class Password {
private static readonly char[] _Letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
private static readonly char[] _Numbers = "1234567890".ToCharArray();
private static readonly char[] _Symbols = "!@#$%^&*.?".ToCharArray();
int _MinimumLength, _MaximumLength;
bool _IncludeUpper, _IncludeLower, _IncludeNumber, _IncludeSpecial;
string[] _CharacterTypes;
enum CharacterType {
Uppercase,
Lowercase,
Special,
Number
}
public bool IncludeUpper {
get {
return _IncludeUpper;
}
set {
_IncludeUpper = value;
}
}
public bool IncludeLower {
get {
return _IncludeLower;
}
set {
_IncludeLower = value;
}
}
public bool IncludeNumber {
get {
return _IncludeNumber;
}
set {
_IncludeNumber = value;
}
}
public bool IncludeSpecial {
get {
return _IncludeSpecial;
}
set {
_IncludeSpecial = value;
}
}
public int MinimumLength {
get {
return _MinimumLength;
}
set {
if (value > _MaximumLength) {
throw new ArgumentOutOfRangeException("MinimumLength must be greater than MaximumLength");
}
_MinimumLength = value;
}
}
public int MaximumLength {
get {
return _MaximumLength;
}
set {
if (value < _MinimumLength) {
throw new ArgumentOutOfRangeException("MaximumLength must be greater than MinimumLength");
}
_MaximumLength = value;
}
}
public Password() {
_MinimumLength = 6;
_MaximumLength = 20;
_IncludeSpecial = true;
_IncludeNumber = true;
_IncludeUpper = true;
_IncludeLower = true;
}
public Password(bool includeSpecial, bool includeNumber, bool includeUpper, bool includeLower)
: this() {
_IncludeNumber = includeNumber;
_IncludeSpecial = includeSpecial;
_IncludeUpper = includeUpper;
_IncludeLower = includeLower;
}
/// <summary>
/// Randomly creates a password.
/// </summary>
/// <returns>A random string of characters.</returns>
public string Create() {
_CharacterTypes = getCharacterTypes();
StringBuilder password = new StringBuilder(_MaximumLength);
//Get a random length for the password.
int currentPasswordLength = RandomNumber.Next(_MaximumLength);
//Only allow for passwords greater than or equal to the minimum length.
if (currentPasswordLength < _MinimumLength) {
currentPasswordLength = _MinimumLength;
}
//Generate the password
for (int i = 0; i < currentPasswordLength; i++) {
password.Append(getCharacter());
}
return password.ToString();
}
/// <summary>
/// Determines which character types should be used to generate
/// the current password.
/// </summary>
/// <returns>A string[] of character that should be used to generate the current password.</returns>
private string[] getCharacterTypes() {
ArrayList characterTypes = new ArrayList();
foreach (string characterType in Enum.GetNames(typeof(CharacterType))) {
CharacterType currentType = (CharacterType)Enum.Parse(typeof(CharacterType), characterType, false);
bool addType = false;
switch (currentType) {
case CharacterType.Lowercase:
addType = IncludeLower;
break;
case CharacterType.Number:
addType = IncludeNumber;
break;
case CharacterType.Special:
addType = IncludeSpecial;
break;
case CharacterType.Uppercase:
addType = IncludeUpper;
break;
}
if (addType) {
characterTypes.Add(characterType);
}
}
return (string[])characterTypes.ToArray(typeof(string));
}
/// <summary>
/// Randomly determines a character type to return from the
/// available CharacterType enum.
/// </summary>
/// <returns>The string character to append to the password.</returns>
private string getCharacter() {
string characterType = _CharacterTypes[RandomNumber.Next(_CharacterTypes.Length)];
CharacterType typeToGet = (CharacterType)Enum.Parse(typeof(CharacterType), characterType, false);
switch (typeToGet) {
case CharacterType.Lowercase:
return _Letters[RandomNumber.Next(_Letters.Length)].ToString().ToLower();
case CharacterType.Uppercase:
return _Letters[RandomNumber.Next(_Letters.Length)].ToString().ToUpper();
case CharacterType.Number:
return _Numbers[RandomNumber.Next(_Numbers.Length)].ToString();
case CharacterType.Special:
return _Symbols[RandomNumber.Next(_Symbols.Length)].ToString();
}
return null;
}
}
public sealed class RandomNumber {
private static RNGCryptoServiceProvider _Random = new RNGCryptoServiceProvider();
private static byte[] bytes = new byte[4];
private RandomNumber() { }
public static int Next(int max) {
if (max <= 0) {
throw new ArgumentOutOfRangeException("max");
}
_Random.GetBytes(bytes);
int value = BitConverter.ToInt32(bytes, 0) % max;
if (value < 0) {
value = -value;
}
return value;
}
}
}