Advertisement
Guest User

Untitled

a guest
Jun 11th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9.  
  10. namespace ConsoleApplication1
  11. {
  12.     struct Stats
  13.     {
  14.         public string LastValue;
  15.         public long Frist;
  16.         public long Last;
  17.         public long Progress;
  18.         public double Speed;
  19.     }
  20.  
  21.     class Worker
  22.     {
  23.         public bool Stop { get; set; }
  24.         public Stats stats = new Stats();
  25.         public byte[] Target { get; set; }
  26.         public bool Success { get; private set; }
  27.         public String Value { get; private set; }
  28.         long _start, _stop;
  29.  
  30.         public Worker(long start, long stop, byte[] target)
  31.         {
  32.             _start = start;
  33.             _stop = stop;
  34.             stats.Progress = start;
  35.             stats.Frist = start;
  36.             stats.Last = stop;
  37.             stats.LastValue = "";
  38.             Target = target;
  39.             Stop = false;
  40.         }
  41.  
  42.         public void Start()
  43.         {
  44.             perm p = new perm(stats.Frist);
  45.             byte[] hash = new byte[16];
  46.             Stopwatch watch = new Stopwatch();
  47.             watch.Start();
  48.             hash = Converter.HexToByteArray("202cb962ac59075b964b07152d234b70");
  49.             for (long i = _start; i < _stop; i++)
  50.             {
  51.                 hash = MD5.Create().ComputeHash(Encoding.Default.GetBytes(p.ValueAsCharArray));
  52.                 //MD5.Create().ComputeHash(Encoding.Default.GetBytes(p.ValueAsCharArray)).CopyTo(hash, 0);
  53.                 if (hash.SequenceEqual(Target))
  54.                 {
  55.                     Success = true;
  56.                     Value = p.Value;
  57.                     break;
  58.                 }
  59.                 p.Next();
  60.                 if (i % 10000 == 0)
  61.                 {
  62.                     //GC.Collect();
  63.                     try
  64.                     {
  65.                         stats.Speed = 1000000 / watch.ElapsedMilliseconds; //interval * 1000
  66.                     }
  67.                     catch (DivideByZeroException e)
  68.                     {
  69.                         stats.Speed = 0;
  70.                     }
  71.                     watch.Restart();
  72.                     stats.Progress = i;
  73.                     stats.LastValue = p.Value;
  74.                     if (Stop) break;
  75.                 }
  76.             }
  77.         }
  78.     }
  79.  
  80.     class Program
  81.     {
  82.         static void Main(string[] args)
  83.         {
  84.             DateTime nu = DateTime.Now;
  85.  
  86.             string s = Workworkwork();
  87.             Console.Clear();
  88.             Console.WriteLine(s);
  89.  
  90.             Console.WriteLine((DateTime.Now - nu).TotalMilliseconds);
  91.  
  92.             Console.ReadKey();
  93.         }
  94.  
  95.         public static string Workworkwork()
  96.         {
  97.             int interval = 500000, iCount = 0;
  98.             int threadCount = Environment.ProcessorCount - 1;
  99.             // 63363e9525ee0fe5ce26689544a7368d // hejja
  100.             // 365beccc2337a59965fd189105f1d4f3 // armera
  101.             // 05b972dcf28374406d13e879724bfe3b // hejsan
  102.             byte[] target = Converter.HexToByteArray("63363e9525ee0fe5ce26689544a7368d");
  103.             Worker[] workers = new Worker[threadCount];
  104.             for (int i = 0; i < threadCount; i++)
  105.                 workers[i] = new Worker(iCount++ * interval, iCount * interval, target);
  106.  
  107.             Thread[] threads = new Thread[threadCount];
  108.             for (int i = 0; i < threadCount; i++)
  109.                 threads[i] = new Thread(new ThreadStart(workers[i].Start));
  110.  
  111.             foreach (Thread t in threads)
  112.                 t.Start();
  113.  
  114.             double percent, sumSpeed;
  115.  
  116.             Stopwatch watch = new Stopwatch();
  117.             watch.Start();
  118.             while (true)
  119.             {
  120.                 sumSpeed = 0;
  121.                 for (int i = 0; i < threadCount; i++)
  122.                 {
  123.                     sumSpeed += workers[i].stats.Speed;
  124.                     Console.CursorLeft = 2;
  125.                     Console.CursorTop = 3 + i * 3;
  126.                     Console.Write("Thread " + i);
  127.                     Console.WriteLine(" n: {0:n0} = {1} @{2:n0}/s    ",
  128.                         workers[i].stats.Progress, workers[i].stats.LastValue, workers[i].stats.Speed);
  129.  
  130.                     percent = 100 * (workers[i].stats.Progress - workers[i].stats.Frist) / (workers[i].stats.Last - workers[i].stats.Frist);
  131.                     Console.CursorLeft = 2;
  132.                     Console.BackgroundColor = ConsoleColor.DarkGray;
  133.                     Console.Write(new string(' ', 76));
  134.  
  135.                     Console.CursorLeft = 2;
  136.                     Console.BackgroundColor = ConsoleColor.Cyan;
  137.                     Console.Write(new string(' ', (int)(percent / 1.3)));
  138.                     Console.ResetColor();
  139.  
  140.                     if (!threads[i].IsAlive)
  141.                     {
  142.                         if (workers[i].Success)
  143.                         {
  144.                             foreach (Worker w in workers)
  145.                                 w.Stop = true;
  146.                             return workers[i].Value;
  147.                         }
  148.                         else
  149.                         {
  150.                             workers[i] = new Worker(iCount++ * interval, iCount * interval, target);
  151.                             threads[i] = new Thread(new ThreadStart(workers[i].Start));
  152.                             threads[i].Start();
  153.                         }
  154.                     }
  155.                 }
  156.                 Console.CursorTop = 0;
  157.                 Console.CursorLeft = 2;
  158.                 Console.WriteLine("{0,-10}{1,-20:n0}{2,46}", "Hashes/s:", sumSpeed, watch.Elapsed.ToString(@"hh\:mm\:ss"));
  159.                 Thread.Sleep(200);
  160.             }
  161.         }
  162.  
  163.        
  164.     }
  165.  
  166.     class perm
  167.     {
  168.         static char[] _alpha = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray();
  169.         //static char[] _alpha = "0123456789".ToCharArray();
  170.  
  171.         char[] _value;
  172.         public string Value
  173.         {
  174.             get
  175.             {
  176.                 return new String(_value);
  177.             }
  178.             private set
  179.             {
  180.                 _value = value.ToCharArray();
  181.             }
  182.         }
  183.  
  184.         public char[] ValueAsCharArray
  185.         {
  186.             get { return _value; }
  187.             private set { }
  188.         }
  189.  
  190.         public static char[] l2c(long lng)
  191.         {
  192.             long l = lng;
  193.             int permBase = _alpha.Count();
  194.  
  195.             int pow = 0;
  196.             long tmp = 0, prev = 0;
  197.             do
  198.             {
  199.                 prev = tmp;
  200.                 pow++;
  201.                 tmp += (long)Math.Pow(permBase, pow);
  202.  
  203.             } while (l >= tmp);
  204.             if (pow > 1)
  205.                 l -= prev;
  206.  
  207.             char[] cArr = new char[pow];
  208.  
  209.             for (int i = pow - 1; i >= 0; i--)
  210.             {
  211.                 int j = 0;
  212.                 long val = (long)Math.Pow(permBase, i);
  213.                 while (l >= val)
  214.                 {
  215.                     l -= val;
  216.                     j++;
  217.                 }
  218.                 cArr[pow - i - 1] = _alpha[j];
  219.             }
  220.             return cArr;
  221.         }
  222.  
  223.         public perm(long val)
  224.         {
  225.             _value = l2c(val);
  226.         }
  227.  
  228.         public char[] Next()
  229.         {
  230.             int i = 0, j = 0;
  231.             for (i = _value.Length - 1; i >= 0; i--)
  232.             {
  233.                 for (j = 0; j < _alpha.Length; j++)
  234.                     if (_alpha[j] == _value[i]) break;
  235.  
  236.                 if (++j != _alpha.Length)
  237.                 {
  238.                     _value[i] = _alpha[j];
  239.                     break;
  240.                 }
  241.                 else
  242.                 {
  243.                     _value[i] = _alpha[0];
  244.                 }
  245.             }
  246.             if (i == -1 && j == _alpha.Length)
  247.             {
  248.                 Array.Resize(ref _value, _value.Length + 1);
  249.                 _value[_value.Length - 1] = _alpha[0];
  250.             }
  251.             return _value;
  252.         }
  253.     }
  254.  
  255.     static class Converter
  256.     {
  257.         public static byte[] HexToByteArray(string hex)
  258.         {
  259.             int len = hex.Length;
  260.             byte[] bytes = new byte[len / 2];
  261.             for (int i = 0; i < len; i += 2)
  262.                 bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
  263.             return bytes;
  264.         }
  265.     }
  266.        
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement