using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
namespace BezpieczenstwoSystemowKomputerowych2103
{
class Program
{
static void Main(string[] args)
{
string source = "Hello World!";
using (MD5 md5Hash = MD5.Create())
{
string hash = GetMD5Hash(md5Hash, source);
Console.WriteLine("The MD5 hash of: " + source + " is: " + hash + ".");
Console.WriteLine("Veryfing the hash...");
if (VerifyMd5Hash(md5Hash, source, hash))
{
Console.WriteLine("The hashes are the same.");
}
else
{
Console.WriteLine("The hashes are not the same.");
}
}
Console.ReadKey();
}
static string GetMD5Hash(MD5 md5Hash, string input)
{
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
StringBuilder sBuilder
= new StringBuilder
();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash)
{
string hashOfInput = GetMD5Hash(md5Hash, input);
StringComparer comparer = StringComparer.OrdinalIgnoreCase;
if (0 == comparer.Compare(hashOfInput, hash))
{
return true;
}
else
{
return false;
}
}
/*
string source = ""; //f37981b90e71dcb98efa0539c8a5bbd4. //7215ee9c7d9dc229d2921a40e899ec5f
using (MD5 md5Hash = MD5.Create())
{
string hash = GetMD5Hash(md5Hash, source);
Console.WriteLine("The MD5 hash of: " + source + " is: " + hash + ".");
Console.ReadKey();
}
}
static string GetMD5Hash(MD5 md5Hash, string input) {
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++) {
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
/*
const int totalRolls = 25000;
int[] results = new int[6];
for (int x = 0; x < totalRolls; x++)
{
byte roll = RollDice((byte)results.Length);
results[roll - 1]++;
}
for (int i = 0; i < results.Length; ++i)
{
Console.WriteLine("{0}: {1} ({2:p1})", i + 1, results[i], (double)results[i] / (double)totalRolls);
}
Console.ReadKey();
}
public static byte RollDice(byte numberSides)
{
if (numberSides <= 0)
{
throw new ArgumentOutOfRangeException("numberSides");
}
RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
byte[] randomNumber = new byte[1];
do
{
rngCsp.GetBytes(randomNumber);
} while (!isFairRoll(randomNumber[0], numberSides));
return (byte)((randomNumber[0] % numberSides) + 1);
}
private static bool isFairRoll(byte roll, byte numSides)
{
int fullSetsOfValeus = Byte.MaxValue / numSides;
return roll < numSides * fullSetsOfValeus;
}
/*
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
byte[] data = new byte[4]; //deklaracja tablicy bufora
for (int i = 0; i < 10; i++)
{
rng.GetBytes(data); //wypełnienie bufora
int value = BitConverter.ToInt32(data, 0); //konwersja to int32
Console.WriteLine(value);
}
}
/*
byte[] bytes1 = new byte[100];
byte[] bytes2 = new byte[100];
Random rnd1 = new Random();
Random rnd2 = new Random();
rnd1.NextBytes(bytes1);
rnd2.NextBytes(bytes2);
Console.WriteLine("Pierwsza seria liczb: ");
for (int ctr = bytes1.GetLowerBound(0); ctr <= bytes1.GetUpperBound(0); ctr++)
{
Console.Write("{0,5 }", bytes1[ctr]);
if ((ctr + 1) % 10 == 0)
{
Console.WriteLine();
}
}
Console.WriteLine("Druga seria liczb: ");
for (int ctr = bytes2.GetLowerBound(0); ctr <= bytes2.GetUpperBound(0); ctr++)
{
Console.Write("{0,5 }", bytes2[ctr]);
if ((ctr + 1) % 10 == 0)
{
Console.WriteLine();
}
}
/*
Random rand = new Random();
Console.WriteLine("Pięc losowych liczb zmiennoprzecinkowych z zakresu 0-1: ");
for (int ctr = 0; ctr <= 4; ctr++)
{
Console.WriteLine("{0,8:N3}", rand.NextDouble());
}
Console.WriteLine();
/*
Random rand = new Random();
Console.Write("Pięc losowych liczb całkowitych z zakresu 0-100");
for (int i = 0; i < 5; i++)
{
Console.Write("{0,8:N0}", rand.Next(101));
}
Console.WriteLine();
/*
Random r = new Random();
int randomNumber = r.Next(1, 100);
Console.Write("Liczba losowa wynosi: {0}", randomNumber);
*/
//Console.ReadKey();
}
}