Advertisement
Guest User

Untitled

a guest
Sep 26th, 2014
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Threading;
  6.  
  7. namespace Hashes
  8. {
  9.     class CLI
  10.     {
  11.         class HashState
  12.         {
  13.             public float Progress { get; private set; }
  14.             public bool Completed { get; private set; }
  15.             public string Result { get; private set; }
  16.  
  17.             public HashState(Hasher.Hash hash)
  18.             {
  19.                 hash.OnProgressChanged += (object sender, float progress) => Progress = progress;
  20.                 hash.OnCompleted += (object sender, byte[] result) =>
  21.                     {
  22.                         Completed = true;
  23.                         Result = BitConverter.ToString(result).Replace("-", "").ToLower();
  24.                     };
  25.             }
  26.         }
  27.  
  28.         Dictionary<string, HashState> hashStates;
  29.         CancellationTokenSource cancellationTokenSource;
  30.  
  31.         public CLI(Dictionary<string, Hasher.Hash> hashes, CancellationTokenSource cancellationTokenSource)
  32.         {
  33.             this.cancellationTokenSource = cancellationTokenSource;
  34.  
  35.             hashStates = new Dictionary<string, HashState>();
  36.  
  37.             foreach (var hash in hashes)
  38.                 hashStates[hash.Key] = new HashState(hash.Value);
  39.  
  40.             Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) =>
  41.                 {
  42.                     if (!cancellationTokenSource.IsCancellationRequested)
  43.                     {
  44.                         cancellationTokenSource.Cancel(true);
  45.                         e.Cancel = true;
  46.                     }
  47.                 };
  48.         }
  49.  
  50.         public void PrintProgress()
  51.         {
  52.             while (true)
  53.             {
  54.                 Console.Clear();
  55.  
  56.                 foreach (var hashState in hashStates)
  57.                 {
  58.                     var state = hashState.Value;
  59.  
  60.                     Console.Write("{0}: ", hashState.Key);
  61.  
  62.                     if (state.Completed)
  63.                         Console.WriteLine(state.Result);
  64.                     else
  65.                         Console.WriteLine(state.Progress.ToString("P"));
  66.                 }
  67.  
  68.                 if (hashStates.Values.All(s => s.Completed))
  69.                 {
  70.                     Console.Clear();
  71.                     PrintResults();
  72.                     break;
  73.                 }
  74.                 else if (cancellationTokenSource.IsCancellationRequested)
  75.                     Console.WriteLine("Aborted");
  76.                 else
  77.                     Console.WriteLine("Hashing...");
  78.  
  79.                 Thread.Sleep(50);
  80.             }
  81.         }
  82.  
  83.         public void PrintResults()
  84.         {
  85.             foreach (var hashState in hashStates)
  86.                 Console.WriteLine("{0}: {1}", hashState.Key, hashState.Value.Result);
  87.         }
  88.     }
  89.    
  90.     static class Program
  91.     {
  92.         static void Main(string[] args)
  93.         {
  94.             if (args.Length < 1)
  95.                 return;
  96.  
  97.             var hashes = new Dictionary<string, Hasher.Hash>()
  98.             {
  99.                 { "MD5", Hasher.CreateHash(() => MD5.Create()) },
  100.                 { "SHA-1", Hasher.CreateHash<SHA1Managed>() },
  101.                 { "SHA-256", Hasher.CreateHash<SHA256Managed>() },
  102.                 { "SHA-384", Hasher.CreateHash<SHA384Managed>() },
  103.                 { "SHA-512", Hasher.CreateHash<SHA512Managed>() }
  104.             };
  105.  
  106.             var cancellationTokenSource = new CancellationTokenSource();
  107.  
  108.             var task = Hasher.HashAsync(hashes.Values, args[0], cancellationTokenSource.Token);
  109.             task.Start();
  110.  
  111.             var cli = new CLI(hashes, cancellationTokenSource);
  112.  
  113.             if (Console.IsOutputRedirected)
  114.             {
  115.                 task.Wait();
  116.  
  117.                 cli.PrintResults();
  118.             }
  119.             else
  120.             {
  121.                 cli.PrintProgress();
  122.             }
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement