-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomStringGenerator.cs
More file actions
103 lines (92 loc) · 3.83 KB
/
RandomStringGenerator.cs
File metadata and controls
103 lines (92 loc) · 3.83 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
using System;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LnDCSharpProj1
{
class RandomStringGenerator
{
public void GenerateRandomString()
{
const int length = 20;
bool isValidPass;
string fileName = @"C:\NonNetworkedArea\Sandbox\C#\randomStrings.txt";
var random = new Random();
var buffer = new char[length];
string runAgain;
do
{
isValidPass = false;
while (isValidPass == false)
{
for (var i = 0; i < length; i++)
{
buffer[i] = (char)(random.Next(33, 122));
}
//validate that at least one of each from the ranges below is in the buffer
var lowerCaseCounter = 0;
var upperCaseCounter = 0;
var specialCharCounter = 0;
var numberCounter = 0;
foreach (var number in buffer)
{
if (number >= 33 && number <= 47)
{
specialCharCounter = specialCharCounter + 1;
}
else if (number >= 48 && number <= 57)
{
numberCounter = numberCounter + 1;
}
else if (number >= 58 && number <= 64)
{
specialCharCounter = specialCharCounter + 1;
}
else if (number >= 65 && number <= 90)
{
upperCaseCounter = upperCaseCounter + 1;
}
else if (number >= 97 && number <= 122)
{
lowerCaseCounter = lowerCaseCounter + 1;
}
}
if (lowerCaseCounter == 0 || upperCaseCounter == 0 || specialCharCounter == 0 || numberCounter == 0)
{
isValidPass = false;
}
else
{
isValidPass = true;
}
//for (var i = 16; i < length; i++)
//{
// buffer[i] = (char)(random.Next())) // special char ascii 33 - 47, 58 - 64, //65 - 90 for upper case
// lower case is 97 - 122
// numbers are 48 - 57
//}
}
var password = new string(buffer);
Console.WriteLine("Password is {0}. Copied password to clipboard.", password);
Clipboard.SetText(password);
Console.WriteLine("Appended Password to file {0}, for safekeeping. \nTherefore don't use these as actual passwords.", fileName);
using (StreamWriter sw = File.AppendText(fileName))
{
sw.WriteLine(password);
}
Console.WriteLine("Would you like to generate another Y/n?");
runAgain = Console.ReadLine().ToLower();
//runAgain = "y".Equals(input, StringComparison.OrdinalIgnoreCase);
} while (runAgain.Equals("y", StringComparison.OrdinalIgnoreCase));
{
Application.Exit();
//Console.ReadLine();
//runAgain = "y".Equals(input, StringComparison.OrdinalIgnoreCase);
//isValidPass = false;
}
}
}
}