Advertisement
Shokedbrain

Untitled

Jun 8th, 2021
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5.  
  6. namespace artem_laba
  7. {
  8.     class Program
  9.     {
  10.         public static string PrintBytes(byte[] hashBytes)
  11.         {
  12.             StringBuilder sb = new StringBuilder();
  13.             foreach (var t in hashBytes)
  14.             {
  15.                 sb.Append(t.ToString("X2").ToLower());
  16.             }
  17.             return sb.ToString();
  18.         }
  19.  
  20.         public static string GenMD5(string inp)
  21.         {
  22.             MD5 md5 = MD5.Create();
  23.             byte[] hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(inp));
  24.             return PrintBytes(hashBytes);
  25.         }
  26.  
  27.         static void Main(string[] args)
  28.         {
  29.             // open file
  30.             string path;
  31.             do
  32.             {
  33.                 Console.Write("Enter path: ");
  34.                 path = Console.ReadLine();
  35.                 path = path.Trim();
  36.             } while (path == "");
  37.  
  38.             // проверка абсолютный или относительный путь
  39.             bool absolute = path.Contains('\\');
  40.             if (!absolute)
  41.                 path = Path.GetFullPath(path).ToString();    
  42.             Console.WriteLine(path);
  43.             // читаем из файла
  44.             try
  45.             {
  46.                 using StreamReader sr = new StreamReader(path);
  47.                 string context = sr.ReadToEnd();
  48.                 // шифруем
  49.                 string encrypted = GenMD5(context);
  50.                 Console.WriteLine(encrypted);
  51.             }
  52.             catch (Exception e)
  53.             {
  54.                 Console.WriteLine(e.Message);
  55.             }
  56.         }
  57.     }
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement