Advertisement
Guest User

hashgenerator

a guest
Jul 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Security.Cryptography;
  8.  
  9. namespace HashEvaluator
  10. {
  11.     class Program
  12.     {
  13.         static string Calc_md5(string input)
  14.         {
  15.             // step 1, calculate MD5 hash from input
  16.             MD5 md5 = System.Security.Cryptography.MD5.Create();
  17.             byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
  18.             byte[] hash = md5.ComputeHash(inputBytes);
  19.  
  20.             // step 2, convert byte array to hex string
  21.             StringBuilder sb = new StringBuilder();
  22.             for (int i = 0; i < hash.Length; i++)
  23.             {
  24.                 sb.Append(hash[i].ToString("X2"));
  25.             }
  26.             return sb.ToString();
  27.         }
  28.         static string Calc_sha1(string input)
  29.         {
  30.             SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
  31.             byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
  32.             byte[] hash = sha1.ComputeHash(inputBytes);
  33.  
  34.             // step 2, convert byte array to hex string
  35.             StringBuilder sb = new StringBuilder();
  36.             for (int i = 0; i < hash.Length; i++)
  37.             {
  38.                 sb.Append(hash[i].ToString("X2"));
  39.             }
  40.             return sb.ToString();
  41.         }
  42.  
  43.         static string file_MD5(string path)
  44.         {
  45.             using (var md5 = MD5.Create())
  46.             {
  47.                 using (var stream = File.OpenRead(path))
  48.                 {
  49.                     var hash = md5.ComputeHash(stream);
  50.                     return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
  51.                 }
  52.             }
  53.         }
  54.  
  55.         static string file_SHA1(string path)
  56.         {
  57.             using (var sha1 = SHA1.Create())
  58.             {
  59.                 using (var stream = File.OpenRead(path))
  60.                 {
  61.                     var hash = sha1.ComputeHash(stream);
  62.                     return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
  63.                 }
  64.             }
  65.         }
  66.         static void Main(string[] args)
  67.         {
  68.             Console.Write("Enter in string: ");
  69.             string input = Console.ReadLine();
  70.             Console.WriteLine("MD5 HASH: " + Calc_md5(input));
  71.             Console.WriteLine("SHA1 HASH: " + Calc_sha1(input));
  72.             Console.Write("Enter a filepath: ");
  73.             input = Console.ReadLine();
  74.             Console.WriteLine("MD5 HASH: " + file_MD5(input));
  75.             Console.WriteLine("SHA1 HASH: " + file_SHA1(input));
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement