-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PassGenerator.cs
221 lines (196 loc) · 8.46 KB
/
PassGenerator.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
using System;
using System.Linq;
using Windows.Storage;
using System.Security.Cryptography;
using Windows.UI.Xaml.Controls;
namespace PassDefend
{
class PassGenerator
{
//function to prepare to generate passwords
public static void regenerateGeneratedPass(Button regenerateGeneratedButton, Button storeGeneratedButton, CheckBox generateLowercaseOption, CheckBox generateCapitalsOption, CheckBox generateNumbersOption, CheckBox generateSymbolsOption, Slider generateLengthSlider, TextBox generateResultBox)
{
//set valid characters to 0
string validCharacters = "";
//list available characters
const string lowercases = "abcdefghijklmnopqrstuvwxyz";
const string uppercases = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string numbers = "0123456789";
const string symbols = "@!#%$*&^?";
//determine the valid characters from the selected options
if ((bool)generateLowercaseOption.IsChecked == true)
{
validCharacters += lowercases;
}
if ((bool)generateCapitalsOption.IsChecked == true)
{
validCharacters += uppercases;
}
if ((bool)generateNumbersOption.IsChecked == true)
{
validCharacters += numbers;
}
if ((bool)generateSymbolsOption.IsChecked == true)
{
validCharacters += symbols;
}
//if no options are selected...
if ((bool)generateSymbolsOption.IsChecked == false & (bool)generateNumbersOption.IsChecked == false & (bool)generateCapitalsOption.IsChecked == false & (bool)generateLowercaseOption.IsChecked == false)
{
//cancel the regeneration and lock the buttons
generateResultBox.Text = "Select some options";
regenerateGeneratedButton.IsEnabled = false;
storeGeneratedButton.IsEnabled = false;
return;
}
else //else if options are selected...
{
//unlock the buttons
regenerateGeneratedButton.IsEnabled = true;
storeGeneratedButton.IsEnabled = true;
}
//generate the string
string generatedPassword = generatePassword(validCharacters, generateLengthSlider);
//check if the string contains a symbol if symbols are required, and regenerate if not
if ((bool)generateSymbolsOption.IsChecked == true)
{
while (!generatedPassword.Any(c => symbols.Contains(c)))
{
generatedPassword = generatePassword(validCharacters, generateLengthSlider);
}
}
//check if the string contains a number if numbers are required, and regenerate if not
if ((bool)generateNumbersOption.IsChecked == true)
{
while (!generatedPassword.Any(c => numbers.Contains(c)))
{
generatedPassword = generatePassword(validCharacters, generateLengthSlider);
}
}
//check if the string contains a capital if capitals are required, and regenerate if not
if ((bool)generateCapitalsOption.IsChecked == true)
{
while (!generatedPassword.Any(c => uppercases.Contains(c)))
{
generatedPassword = generatePassword(validCharacters, generateLengthSlider);
}
}
//check if the string contains a lowercase if lowercases are required, and regenerate if not
if ((bool)generateLowercaseOption.IsChecked == true)
{
while (!generatedPassword.Any(c => lowercases.Contains(c)))
{
generatedPassword = generatePassword(validCharacters, generateLengthSlider);
}
}
//send the generated string to the results box
generateResultBox.Text = generatedPassword;
}
//function to generate a password using the prep work from the previous function
private static string generatePassword(string validCharacters, Slider generateLengthSlider)
{
string generatedPass = "";
using (RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider())
{
while (generatedPass.Length != generateLengthSlider.Value)
{
{
byte[] oneByte = new byte[1];
provider.GetBytes(oneByte);
char character = (char)oneByte[0];
if (validCharacters.Contains(character))
{
generatedPass += character;
}
}
}
}
return generatedPass;
}
public static async void saveSettings(CheckBox generateLowercaseOption, CheckBox generateCapitalsOption, CheckBox generateNumbersOption, CheckBox generateSymbolsOption, Slider generateLengthSlider)
{
//prep storage
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile genSettings = await localFolder.CreateFileAsync("genSettings", CreationCollisionOption.OpenIfExists);
//prepare to write the settings line to file for future use
/*string format: each digit either 1 or 0, 1 being on, 0 being off, except for length. Example:
* 1 0 1 0 16
* ^ ^ ^ ^ ^
* | | | | |
* | | | | length is 16
* | | | symbols are off
* | | numbers are on
* | lowercase is off
* uppercase is on
*/
string settings = "";
//this allows the string to be written to file, then read back when the program is reopened to keep the users choices.
if ((bool)generateLowercaseOption.IsChecked == true)
{
settings += "1 ";
}
else
{
settings += "0 ";
}
if ((bool)generateCapitalsOption.IsChecked == true)
{
settings += "1 ";
}
else
{
settings += "0 ";
}
if ((bool)generateNumbersOption.IsChecked == true)
{
settings += "1 ";
}
else
{
settings += "0 ";
}
if ((bool)generateSymbolsOption.IsChecked == true)
{
settings += "1 ";
}
else
{
settings += "0 ";
}
settings += generateLengthSlider.Value;
//save the generation settings for future sessions
await FileIO.WriteTextAsync(genSettings, settings);
}
public static async void loadSettings(CheckBox generateLowercaseOption, CheckBox generateCapitalsOption, CheckBox generateNumbersOption, CheckBox generateSymbolsOption, Slider generateLengthSlider)
{
//prep storage
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
if (await localFolder.TryGetItemAsync("genSettings") != null) //if generator settings exist
{
//read file
StorageFile settingsfile = await localFolder.GetFileAsync("genSettings");
string fileContent = await FileIO.ReadTextAsync(settingsfile);
//read each number and act accordingly
//order: lowercase, capitals, numbers, symbols, length
string[] settingentries = fileContent.Split(' ');
if (settingentries[0] == "0")
{
generateLowercaseOption.IsChecked = false;
}
if (settingentries[1] == "0")
{
generateCapitalsOption.IsChecked = false;
}
if (settingentries[2] == "0")
{
generateNumbersOption.IsChecked = false;
}
if (settingentries[3] == "0")
{
generateSymbolsOption.IsChecked = false;
}
generateLengthSlider.Value = Double.Parse(settingentries[4]);
}
}
}
}