-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
114 lines (102 loc) · 3.92 KB
/
Program.cs
File metadata and controls
114 lines (102 loc) · 3.92 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
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using PassGenLogic;
namespace RandomPasswordGenerator
{
class Program
{
static string newPassword;
static PasswordGenerator passGen = new PasswordGenerator();
static bool newPasswordCreation = true;
static bool exitApplication;
[STAThread] // this is neeeded to avoid the exception : "Current thread must be set to single thread apartment (sta) mode before ole calls can be made"
//just when executing System.Windows.Forms.Clipboard.SetText(); to copy the password to clipboard!
static void Main(string[] args)
{
while (!exitApplication && newPasswordCreation)
{
DisplayWelcomeMessage();
Console.WriteLine("A strong password must have at least 8 characters.");
newPassword = PasswordCreation();
DisplayPassword(newPassword);
DisplayPasswordMD5Hash(newPassword);
ShowCopyToClipboardHint();
CreateNewPasswordHint();
}
Console.WriteLine();
Console.WriteLine("Press any key to exit the application..");
Console.ReadKey();
}
static void DisplayWelcomeMessage()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("RANDOM PASSWORD GENERATOR!");
Console.WriteLine("=========================");
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.White;
}
static void DisplayPassword(string password)
{
Console.WriteLine("New random password created! --> " + password);
}
static void DisplayPasswordMD5Hash(string password)
{
Console.WriteLine("With an MD5 hash of --> " + passGen.CalcMD5Hash(password));
}
static string PasswordCreation()
{
string password = passGen.CreatePassword();
return password;
}
static void CreateNewPasswordHint()
{
Console.WriteLine("Would you like to create a new random password?");
Console.WriteLine("Y : Yes - N: No, thanks!");
ConsoleKeyInfo key = Console.ReadKey();
Console.Write("\b");
switch (key.Key)
{
case ConsoleKey.Y:
newPasswordCreation = true;
Console.Clear();
break;
case ConsoleKey.N:
Console.WriteLine("Ok, another password won't be created. The application will exit..");
newPasswordCreation = false;
exitApplication = true;
break;
default:
Console.WriteLine("Wrong key pressed, man!");
break;
}
}
static void ShowCopyToClipboardHint()
{
Console.WriteLine("Do you want to copy the password to the clipboard?");
Console.WriteLine("Y : Yes - N: No, thanks!");
ConsoleKeyInfo key = Console.ReadKey();
Console.Write("\b");
switch (key.Key)
{
case ConsoleKey.Enter: // Assume Enter = Y
case ConsoleKey.Y:
CopyPasswordToClipboard(newPassword);
Console.WriteLine("The password has been copied to your clipboard!");
break;
case ConsoleKey.N:
Console.WriteLine("Ok, no password copied!");
break;
default:
Console.WriteLine("Wrong key pressed, man!");
break;
}
}
static void CopyPasswordToClipboard(string stringToCopy)
{
System.Windows.Forms.Clipboard.SetText(stringToCopy);
}
}
}