Facebook
From Insensitive Duck, 5 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 201
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Security.Cryptography;
  7.  
  8. namespace BezpieczenstwoSystemowKomputerowych2103
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string source = "Hello World!";
  15.             using (MD5 md5Hash = MD5.Create())
  16.             {
  17.                 string hash = GetMD5Hash(md5Hash, source);
  18.                 Console.WriteLine("The MD5 hash of: " + source + " is: " + hash + ".");
  19.                 Console.WriteLine("Veryfing the hash...");
  20.                 if (VerifyMd5Hash(md5Hash, source, hash))
  21.                 {
  22.                     Console.WriteLine("The hashes are the same.");
  23.                 }
  24.                 else
  25.                 {
  26.                     Console.WriteLine("The hashes are not the same.");
  27.                 }
  28.  
  29.  
  30.             }
  31.             Console.ReadKey();
  32.         }
  33.  
  34.         static string GetMD5Hash(MD5 md5Hash, string input)
  35.         {
  36.             byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
  37.             StringBuilder sBuilder = new StringBuilder();
  38.             for (int i = 0; i < data.Length; i++)
  39.             {
  40.                 sBuilder.Append(data[i].ToString("x2"));
  41.             }
  42.             return sBuilder.ToString();
  43.  
  44.         }
  45.  
  46.         static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash)
  47.         {
  48.             string hashOfInput = GetMD5Hash(md5Hash, input);
  49.             StringComparer comparer = StringComparer.OrdinalIgnoreCase;
  50.             if (0 == comparer.Compare(hashOfInput, hash))
  51.             {
  52.                 return true;
  53.             }
  54.             else
  55.             {
  56.                 return false;
  57.             }
  58.  
  59.         }
  60.  
  61.  
  62.         /*
  63.         string source = ""; //f37981b90e71dcb98efa0539c8a5bbd4. //7215ee9c7d9dc229d2921a40e899ec5f
  64.         using (MD5 md5Hash = MD5.Create())
  65.         {
  66.             string hash = GetMD5Hash(md5Hash, source);
  67.             Console.WriteLine("The MD5 hash of: " + source + " is: " + hash + ".");
  68.             Console.ReadKey();
  69.         }
  70.  
  71.     }
  72.  
  73.     static string GetMD5Hash(MD5 md5Hash, string input) {
  74.         byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
  75.         StringBuilder sBuilder = new StringBuilder();
  76.         for (int i = 0; i < data.Length; i++) {
  77.             sBuilder.Append(data[i].ToString("x2"));
  78.         }
  79.         return sBuilder.ToString();
  80.     }
  81.  
  82.         /*
  83.         const int totalRolls = 25000;
  84.         int[] results = new int[6];
  85.         for (int x = 0; x < totalRolls; x++)
  86.         {
  87.             byte roll = RollDice((byte)results.Length);
  88.             results[roll - 1]++;
  89.         }
  90.         for (int i = 0; i < results.Length; ++i)
  91.         {
  92.             Console.WriteLine("{0}: {1} ({2:p1})", i + 1, results[i], (double)results[i] / (double)totalRolls);
  93.         }
  94.         Console.ReadKey();
  95.     }
  96.  
  97.     public static byte RollDice(byte numberSides)
  98.     {
  99.         if (numberSides <= 0)
  100.         {
  101.             throw new ArgumentOutOfRangeException("numberSides");
  102.         }
  103.         RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
  104.         byte[] randomNumber = new byte[1];
  105.         do
  106.         {
  107.             rngCsp.GetBytes(randomNumber);
  108.         } while (!isFairRoll(randomNumber[0], numberSides));
  109.         return (byte)((randomNumber[0] % numberSides) + 1);
  110.  
  111.     }
  112.  
  113.     private static bool isFairRoll(byte roll, byte numSides)
  114.     {
  115.         int fullSetsOfValeus = Byte.MaxValue / numSides;
  116.         return roll < numSides * fullSetsOfValeus;
  117.     }
  118.  
  119.  
  120.     /*
  121.     using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
  122.     {
  123.         byte[] data = new byte[4];                              //deklaracja tablicy bufora
  124.         for (int i = 0; i < 10; i++)
  125.         {
  126.             rng.GetBytes(data);                                 //wypełnienie bufora
  127.             int value = BitConverter.ToInt32(data, 0);          //konwersja to int32
  128.             Console.WriteLine(value);
  129.         }
  130.     }
  131.  
  132.     /*
  133.     byte[] bytes1 = new byte[100];
  134.     byte[] bytes2 = new byte[100];
  135.     Random rnd1 = new Random();
  136.     Random rnd2 = new Random();
  137.  
  138.     rnd1.NextBytes(bytes1);
  139.     rnd2.NextBytes(bytes2);
  140.  
  141.     Console.WriteLine("Pierwsza seria liczb: ");
  142.     for (int ctr = bytes1.GetLowerBound(0); ctr <= bytes1.GetUpperBound(0); ctr++)
  143.     {
  144.         Console.Write("{0,5 }", bytes1[ctr]);
  145.         if ((ctr + 1) % 10 == 0)
  146.         {
  147.             Console.WriteLine();
  148.         }
  149.     }
  150.  
  151.     Console.WriteLine("Druga seria liczb: ");
  152.     for (int ctr = bytes2.GetLowerBound(0); ctr <= bytes2.GetUpperBound(0); ctr++)
  153.     {
  154.         Console.Write("{0,5 }", bytes2[ctr]);
  155.         if ((ctr + 1) % 10 == 0)
  156.         {
  157.             Console.WriteLine();
  158.         }
  159.     }
  160.  
  161.     /*
  162.     Random rand = new Random();
  163.  
  164.     Console.WriteLine("Pięc losowych liczb zmiennoprzecinkowych z zakresu 0-1: ");
  165.     for (int ctr = 0; ctr <= 4; ctr++)
  166.     {
  167.         Console.WriteLine("{0,8:N3}", rand.NextDouble());
  168.     }
  169.     Console.WriteLine();
  170.     /*
  171.     Random rand = new Random();
  172.  
  173.     Console.Write("Pięc losowych liczb całkowitych z zakresu 0-100");
  174.     for (int i = 0; i < 5; i++)
  175.     {
  176.         Console.Write("{0,8:N0}", rand.Next(101));
  177.     }
  178.     Console.WriteLine();
  179.     /*
  180.     Random r = new Random();
  181.     int randomNumber = r.Next(1, 100);
  182.     Console.Write("Liczba losowa wynosi: {0}", randomNumber);
  183.     */
  184.  
  185.         //Console.ReadKey();
  186.     }
  187. }
  188.