Skip to content

Commit

Permalink
commit selected files
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailShichko committed Apr 9, 2022
1 parent d0652f2 commit 0369e85
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 9 deletions.
41 changes: 41 additions & 0 deletions Task3/Hash.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace Task3
{
class Hash
{
public string Key { get; private set; }

public string HMAC { get; private set;}

byte[] CreateKey()
{
byte[] key = new byte[32];
var a = RandomNumberGenerator.Create();
a.GetBytes(key);
string keyString = string.Empty;
foreach (byte x in key)
{
keyString += String.Format("{0:x2}", x);
}

this.Key = keyString;
return key;
}

public void CalculateHMAC(string text)
{

HMACSHA256 hashObject = new HMACSHA256(this.CreateKey());
var signature = hashObject.ComputeHash(Encoding.UTF8.GetBytes(text));
var encodedSignature = Convert.ToBase64String(signature);
this.HMAC = encodedSignature;
}
}
}
69 changes: 61 additions & 8 deletions Task3/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Task3
Expand All @@ -7,37 +8,89 @@ class Program
{
static void Main(string[] args)
{
string[] arg = new string[] {"1", "2", "3", "4", "5", "3", "5"};
DoChecks(args);
var t = new Table<string>(args.Length + 1, args.Length + 1);
var table = new Table<string>(args.Length + 1, args.Length + 1);
var rules = new Rules();
rules.FillTable(t, args);
t.ShowTable();
rules.FillTable(table, args);
var hash = new Hash();
string command;
int computerMove;
while (true)
{
computerMove = ComputerMove(args);
hash.CalculateHMAC(args[computerMove]);
Console.WriteLine("HMAC:");
Console.WriteLine(hash.HMAC);
ShowMenu(args);
Console.WriteLine("Enter your move:");
command = Console.ReadLine();
if (command == "?")
{
table.ShowTable();
continue;
}
else if(command == "0") break;
else
{
int yourMove = int.Parse(command);
Console.WriteLine("Your move: {0}", args[yourMove - 1]);
int result = rules.Fight(yourMove - 1, computerMove, args);
Console.WriteLine("Computers move: {0}", args[computerMove]);
if (result == 1) Console.WriteLine("You win!");
else if (result == -1) Console.WriteLine("You lost...");
else Console.WriteLine("Draw.");
Console.WriteLine("HMAC key:");
Console.WriteLine(hash.Key);
}
}
}

private static int ComputerMove(string[] args)
{
var rnd = new Random();
int i = rnd.Next(args.Length);
return i;
}

private static void ShowMenu(string[] args)
{
Console.WriteLine("Available moves:");
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine("{0} - {1}", i+1, args[i]);
}

Console.WriteLine("0 - exit");
Console.WriteLine("? - help");
}

private static void DoChecks(string[] args)
{
var ExceptionList = new List<Exception>();
if (args.Length < 3)
{
throw new ArgumentException("The amount of arguments has to be >= 3. For example: 1 2 3 4 5 6 7 ", nameof(args));
ExceptionList.Add(new ArgumentException("The amount of arguments has to be >= 3. For example: 1 2 3 4 5 6 7 "));
}

if (IsEven(args.Length))
{
throw new ArgumentException("The amount of arguments is not even, but it has to be. For example: 1 2 3 4 5", nameof(args));
ExceptionList.Add(new ArgumentException("The amount of arguments is not even, but it has to be. For example: 1 2 3 4 5"));
}

if (args.Distinct().Count() != args.Length)
{
throw new ArgumentException("There are repeating elements through arguments (all arguments have to be unique). For example: rock scissors paper", nameof(args));
ExceptionList.Add(new ArgumentException("There are repeating elements through arguments (all arguments have to be unique). For example: rock scissors paper"));
}

if (ExceptionList.Any())
{
foreach (var exception in ExceptionList)
{
Console.WriteLine(exception.Message);
}

System.Environment.Exit(0);
}

}

private static bool IsEven(int number)
Expand Down
9 changes: 8 additions & 1 deletion Task3/Rules.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Task3
using System;

namespace Task3
{
class Rules
{
Expand Down Expand Up @@ -32,6 +34,11 @@ public void FillTable(Table<string> table, string[] args)

public int Fight(int ind1, int ind2, string[] args)
{
if (ind1 > args.Length || ind2 > args.Length)
{
throw new ArgumentException();
}

int len = (args.Length - 1) / 2;
if (ind1 < ind2)
{
Expand Down
5 changes: 5 additions & 0 deletions Task3/Task3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BouncyCastle" Version="1.8.9" />
<PackageReference Include="System.Security.Cryptography.Primitives" Version="4.3.0" />
</ItemGroup>

</Project>

0 comments on commit 0369e85

Please sign in to comment.