Skip to content

Commit ae5fb05

Browse files
committed
* Code cleanup
1 parent b33b739 commit ae5fb05

File tree

6 files changed

+247
-218
lines changed

6 files changed

+247
-218
lines changed

Advanced PassGen/Advanced PassGen.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@
7070
<DependentUpon>App.xaml</DependentUpon>
7171
<SubType>Code</SubType>
7272
</Compile>
73-
<Compile Include="Classes\Generator.cs" />
73+
<Compile Include="Classes\PasswordGenerator.cs" />
7474
<Compile Include="Classes\Password.cs" />
75-
<Compile Include="Classes\PasswordAdvisor.cs" />
7675
<Compile Include="Classes\StyleManager.cs" />
7776
<Compile Include="Classes\Update.cs" />
7877
<Compile Include="Classes\UpdateManager.cs" />

Advanced PassGen/Classes/Generator.cs

Lines changed: 0 additions & 91 deletions
This file was deleted.

Advanced PassGen/Classes/PasswordAdvisor.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Security.Cryptography;
6+
using System.Text.RegularExpressions;
7+
using System.Threading.Tasks;
8+
9+
namespace Advanced_PassGen.Classes
10+
{
11+
/// <summary>
12+
/// A class to generate random strings.
13+
/// </summary>
14+
internal class PasswordGenerator
15+
{
16+
#region Variables
17+
18+
private readonly int _minLength;
19+
private readonly int _maxLength;
20+
private readonly int _amount;
21+
private readonly int _seed;
22+
23+
private readonly string _charSet;
24+
25+
private List<Password> _passwordList;
26+
private static Random _rnd = new Random();
27+
28+
#endregion
29+
30+
/// <summary>
31+
/// Initiate a new PasswordGenerator object.
32+
/// </summary>
33+
/// <param name="charSet">The character set that can be used to generate passwords with.</param>
34+
/// <param name="minLength">The minimum length of a password.</param>
35+
/// <param name="maxLength">The maximum length of a password.</param>
36+
/// <param name="amount">The amount of passwords that need to be generated.</param>
37+
/// <param name="seed">The seed for the random number generator.</param>
38+
internal PasswordGenerator(string charSet, int minLength, int maxLength, int amount, int seed)
39+
{
40+
_charSet = charSet;
41+
_minLength = minLength;
42+
_maxLength = maxLength;
43+
_amount = amount;
44+
_seed = seed;
45+
}
46+
47+
/// <summary>
48+
/// Generate a list of passwords.
49+
/// </summary>
50+
/// <returns>A list of passwords.</returns>
51+
internal async Task<List<Password>> GeneratePasswords()
52+
{
53+
_passwordList = new List<Password>();
54+
_rnd = new Random(_seed);
55+
await Task.Run(() =>
56+
{
57+
for (int i = 0; i < _amount; i++)
58+
{
59+
var sub = _rnd.Next(_minLength, _maxLength);
60+
string pwd = GetRandomString(sub, _charSet);
61+
Password pass = new Password
62+
{
63+
ActualPassword = pwd,
64+
Length = pwd.Length,
65+
Strength = CheckStrength(pwd)
66+
};
67+
_passwordList.Add(pass);
68+
}
69+
});
70+
return _passwordList;
71+
}
72+
73+
/// <summary>
74+
/// Generate a random string.
75+
/// </summary>
76+
/// <param name="length">The length of the string that needs to be generated.</param>
77+
/// <param name="characterSet">The characterset that the generator can use.</param>
78+
/// <returns></returns>
79+
private static string GetRandomString(int length, IEnumerable<char> characterSet)
80+
{
81+
char[] characterArray = characterSet.Distinct().ToArray();
82+
byte[] bytes = new byte[length * 8];
83+
new RNGCryptoServiceProvider().GetBytes(bytes);
84+
char[] result = new char[length];
85+
for (int i = 0; i < length; i++)
86+
{
87+
ulong value = BitConverter.ToUInt64(bytes, i * 8);
88+
result[i] = characterArray[value % (uint)characterArray.Length];
89+
}
90+
return new string(result);
91+
}
92+
93+
/// <summary>
94+
/// Check how a strong a password is. The higher the score, the safer the password.
95+
/// </summary>
96+
/// <param name="password">The password that needs to be evaluated.</param>
97+
/// <returns>Returns a password score.</returns>
98+
internal static int CheckStrength(string password)
99+
{
100+
int score = 0;
101+
102+
if (string.IsNullOrEmpty(password)) return 0;
103+
if (password.Length < 1) return 0;
104+
if (password.Length < 4) return 1;
105+
if (password.Length >= 8) score++;
106+
if (password.Length >= 10) score++;
107+
if (Regex.Match(password, @"\d", RegexOptions.ECMAScript).Success) score++;
108+
if (Regex.Match(password, @"[a-z]", RegexOptions.ECMAScript).Success && Regex.Match(password, @"[A-Z]", RegexOptions.ECMAScript).Success) score++;
109+
if (Regex.Match(password, @"[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]", RegexOptions.ECMAScript).Success) score++;
110+
111+
return score;
112+
}
113+
114+
/// <summary>
115+
/// Export a password list as a text file.
116+
/// </summary>
117+
/// <param name="path">The path where the text file should be stored.</param>
118+
internal void ExportText(string path)
119+
{
120+
if (_passwordList.Count < 1) return;
121+
string items = "Password List - Generated by Advanced PassGen" + Environment.NewLine;
122+
for (int i = 0; i < _passwordList.Count; i++)
123+
{
124+
Password pwd = _passwordList[i];
125+
if (pwd == null) continue;
126+
items += pwd.ActualPassword;
127+
if (i != _passwordList.Count - 1)
128+
{
129+
items += Environment.NewLine;
130+
}
131+
}
132+
FileWriter(path, items);
133+
}
134+
135+
/// <summary>
136+
/// Export a password list as a HTML file.
137+
/// </summary>
138+
/// <param name="path">The path where the HTML file should be stored.</param>
139+
internal void ExportHtml(string path)
140+
{
141+
if (_passwordList.Count < 1) return;
142+
string items = "<html><body><h1>Password List - Generated by Advanced PassGen</h1><br /><table><tr><th>Password</th>";
143+
if (Properties.Settings.Default.ExportLength)
144+
{
145+
items += "<th>Length</th>";
146+
}
147+
if (Properties.Settings.Default.ExportStrength)
148+
{
149+
items += "<th>Strength</th>";
150+
}
151+
items += "</tr>";
152+
153+
foreach (Password pwd in _passwordList)
154+
{
155+
if (pwd == null) continue;
156+
items += "<tr><td>" + pwd.ActualPassword + "</td>";
157+
if (Properties.Settings.Default.ExportLength)
158+
{
159+
items += "<td>" + pwd.Length + "</td>";
160+
}
161+
if (Properties.Settings.Default.ExportStrength)
162+
{
163+
items += "<td>" + pwd.Strength + "</td>";
164+
}
165+
items += "</tr>";
166+
}
167+
items += "</table></body></html>";
168+
FileWriter(path, items);
169+
}
170+
171+
/// <summary>
172+
/// Export a password list as a CSV file.
173+
/// </summary>
174+
/// <param name="path">The path where the CSV file should be stored.</param>
175+
internal void ExportCsv(string path)
176+
{
177+
if (_passwordList.Count < 1) return;
178+
string items = "Password List - Generated by Advanced PassGen" + Environment.NewLine + "Password";
179+
if (Properties.Settings.Default.ExportLength)
180+
{
181+
items += Properties.Settings.Default.ExportDelimiter + "Length";
182+
}
183+
if (Properties.Settings.Default.ExportStrength)
184+
{
185+
items += Properties.Settings.Default.ExportDelimiter + "Strength";
186+
}
187+
items += Environment.NewLine;
188+
for (int i = 0; i < _passwordList.Count; i++)
189+
{
190+
Password pwd = _passwordList[i];
191+
if (pwd != null)
192+
{
193+
items += pwd.ActualPassword;
194+
if (Properties.Settings.Default.ExportLength)
195+
{
196+
items += Properties.Settings.Default.ExportDelimiter + pwd.Length;
197+
}
198+
if (Properties.Settings.Default.ExportStrength)
199+
{
200+
items += Properties.Settings.Default.ExportDelimiter + pwd.Strength;
201+
}
202+
}
203+
if (i != _passwordList.Count - 1)
204+
{
205+
items += Environment.NewLine;
206+
}
207+
}
208+
FileWriter(path, items);
209+
}
210+
211+
/// <summary>
212+
/// Write a string to a file.
213+
/// </summary>
214+
/// <param name="path">The path where the string should be stored.</param>
215+
/// <param name="text">The text that should be written to the file.</param>
216+
private static void FileWriter(string path, string text)
217+
{
218+
using (StreamWriter sw = new StreamWriter(path))
219+
{
220+
sw.Write(text);
221+
sw.Close();
222+
}
223+
}
224+
}
225+
}

0 commit comments

Comments
 (0)