-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtectProgram3_4.4.cs
More file actions
144 lines (123 loc) · 4.37 KB
/
ProtectProgram3_4.4.cs
File metadata and controls
144 lines (123 loc) · 4.37 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
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
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
class HashingForUsers
{
static Dictionary<string, byte[]> BaseOfData = new Dictionary<string, byte[]>();
static void Main()
{
int choose;
do
{
Console.WriteLine("~~MENU~~");
Console.WriteLine("1. Sign in!!!");
Console.WriteLine("2. Log in!!!");
Console.WriteLine("3. Removal!!!");
Console.WriteLine("0. Exit");
Console.Write("Choose option---> ");
if (!int.TryParse(Console.ReadLine(), out choose))
{
Console.WriteLine("Invalid input. Please enter a valid option.");
continue;
}
switch (choose)
{
case 1:
RegisterUser();
break;
case 2:
AuthenticateUser();
break;
case 3:
RemoveUser();
break;
case 0:
Environment.Exit(0);
break;
default:
Console.WriteLine("Invalid option. Please select a valid option.");
break;
}
} while (choose != 0);
}
static void RegisterUser()
{
Console.WriteLine("~~REGISTRATION~~~");
Console.Write("Please, Enter login: ");
string login = Console.ReadLine();
if (BaseOfData.ContainsKey(login))
{
Console.WriteLine("Sorry!!! This login is already in use. Please choose a different one.");
return;
}
Console.Write("Please, Enter password: ");
string password = Console.ReadLine();
byte[] passwordInByte = Encoding.UTF8.GetBytes(password);
BaseOfData.Add(login, MD5Hmac(passwordInByte, Encoding.UTF8.GetBytes(login)));
Console.WriteLine("You have been successfully registered!!!");
}
static void AuthenticateUser()
{
Console.WriteLine("~~AUTHORIZATION~~");
Console.Write("Please, Enter login: ");
string login = Console.ReadLine();
if (!BaseOfData.ContainsKey(login))
{
Console.WriteLine("Sorry!!! This login was not found. Please register or check your login.");
return;
}
Console.Write("Please, Enter password: ");
string password = Console.ReadLine();
byte[] passwordInByte = Encoding.UTF8.GetBytes(password);
if (BaseOfData[login].SequenceEqual(MD5Hmac(passwordInByte, Encoding.UTF8.GetBytes(login))))
{
Console.WriteLine($"{login} - successfully authenticated!!!");
}
else
{
Console.WriteLine("Sorry!!! Your password is incorrect.");
}
}
static void RemoveUser()
{
Console.WriteLine("~~REMOVAL USER~~~");
Console.Write("Please, Enter login: ");
string login = Console.ReadLine();
if (BaseOfData.ContainsKey(login))
{
Console.Write("Please, Enter password: ");
string password = Console.ReadLine();
byte[] passwordInByte = Encoding.UTF8.GetBytes(password);
if (BaseOfData[login].SequenceEqual(MD5Hmac(passwordInByte, Encoding.UTF8.GetBytes(login))))
{
Console.Write("Are you sure you want to remove your login and password? (y/n): ");
string answer = Console.ReadLine().ToLower();
if (answer == "y")
{
BaseOfData.Remove(login);
Console.WriteLine("Account was successfully removed!!!");
}
else
{
Console.WriteLine("Account was not removed.");
}
}
else
{
Console.WriteLine("Sorry!!! Your password is incorrect.");
}
}
else
{
Console.WriteLine("Sorry!!! This login was not found.");
}
}
public static byte[] MD5Hmac(byte[] data, byte[] key)
{
using (var md5 = new HMACMD5(key))
{
return md5.ComputeHash(data);
}
}
}