Advertisement
AyrA

Hash Finder

Jun 18th, 2018
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.39 KB | None | 0 0
  1. //https://redd.it/8rx5zi
  2. using System;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Threading;
  9.  
  10. namespace SHAPrefix
  11. {
  12.     class Program
  13.     {
  14.         [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
  15.         private static extern int memcmp(byte[] b1, byte[] b2, int count);
  16.  
  17.         private struct ThreadConfig
  18.         {
  19.             public byte[] Prefix;
  20.         }
  21.  
  22.         private static volatile bool Cont = true;
  23.         private static long Hashes = 0;
  24.         private static object Lockable = new object();
  25.  
  26.         private static byte[] FoundKey;
  27.         private static byte[] FoundHash;
  28.  
  29.         static void Main(string[] args)
  30.         {
  31.             Console.Error.Write("Enter prefix as hexadecimal number [0-9,A-F]: ");
  32.             var Input = Console.ReadLine();
  33.             if (Input.Length % 2 == 0)
  34.             {
  35.                 byte[] Prefix = new byte[Input.Length / 2];
  36.  
  37.                 for (var i = 0; i < Prefix.Length; i++)
  38.                 {
  39.                     Cont &= byte.TryParse(Input.Substring(i * 2, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out Prefix[i]);
  40.                 }
  41.                 if (Cont)
  42.                 {
  43.                     if (Prefix.Length > 0 && Prefix.Length < 33)
  44.                     {
  45.                         Thread[] TList = new Thread[1/*Environment.ProcessorCount*/];
  46.                         for (var i = 0; i < TList.Length; i++)
  47.                         {
  48.                             var T = new Thread(FindPrefix);
  49.                             T.IsBackground = true;
  50.                             T.Priority = ThreadPriority.BelowNormal;
  51.                             T.Name = $"Hash Finder #{i + 1}";
  52.                             TList[i] = T;
  53.                             T.Start(new ThreadConfig() { Prefix = Prefix });
  54.                             Console.Error.WriteLine("Started {0}", T.Name);
  55.                         }
  56.                         while (Cont)
  57.                         {
  58.                             if (TList.Any(m => !m.IsAlive))
  59.                             {
  60.                                 Cont = false;
  61.                             }
  62.                             else
  63.                             {
  64.                                 lock (Lockable)
  65.                                 {
  66.                                     Console.SetCursorPosition(0, TList.Length + 1);
  67.                                     Console.Error.WriteLine("Hashes generated so far: {0}", Hashes);
  68.                                 }
  69.                                 Thread.Sleep(500);
  70.                             }
  71.                         }
  72.                         foreach (var T in TList)
  73.                         {
  74.                             T.Join();
  75.                         }
  76.                         if (FoundKey != null && FoundHash != null)
  77.                         {
  78.                             Console.Clear();
  79.                             Console.WriteLine("Key   : {0}", Encoding.ASCII.GetString(FoundKey));
  80.                             Console.WriteLine("Prefix: {0}", BitConverter.ToString(Prefix).Replace("-", ""));
  81.                             Console.WriteLine("Hash  : {0}", BitConverter.ToString(FoundHash).Replace("-", ""));
  82.                             Console.WriteLine("Check : {0}", Validate(FoundKey, FoundHash));
  83.                         }
  84.                         Console.Error.WriteLine("Process ended");
  85.                     }
  86.                     else
  87.                     {
  88.                         Console.Error.WriteLine("Prefix length is invalid");
  89.                     }
  90.                 }
  91.                 else
  92.                 {
  93.                     Console.Error.WriteLine("Invalid hexadecimal number");
  94.                 }
  95.             }
  96.             else
  97.             {
  98.                 Console.Error.WriteLine("Invalid Prefix. Hexadecimal numbers have an even number of digits");
  99.             }
  100.             Console.ReadKey(true);
  101.         }
  102.  
  103.         static bool Validate(byte[] Key, byte[] Hash)
  104.         {
  105.             using (var Gen = HashAlgorithm.Create("SHA256"))
  106.             {
  107.                 return Hash != null && Hash.Length == 32 && memcmp(Gen.ComputeHash(Key), Hash, Hash.Length) == 0;
  108.             }
  109.         }
  110.  
  111.         static void FindPrefix(object o)
  112.         {
  113.             var RND = new byte[20];
  114.             int StepSize = Environment.ProcessorCount;
  115.             var Config = (ThreadConfig)o;
  116.             var Hash = new byte[32];
  117.  
  118.             using (var RNG = RandomNumberGenerator.Create())
  119.             {
  120.                 RNG.GetBytes(RND);
  121.                 for (var i = 0; i < RND.Length; i += 2)
  122.                 {
  123.                     var S = RND[i].ToString("X2");
  124.                     RND[i] = (byte)S[0];
  125.                     RND[i + 1] = (byte)S[1];
  126.                 }
  127.             }
  128.             using (var Gen = HashAlgorithm.Create("SHA256"))
  129.             {
  130.                 while (Cont)
  131.                 {
  132.                     Hash = Gen.ComputeHash(RND);
  133.                     ++Hashes;
  134.                     if (memcmp(Hash, Config.Prefix, Config.Prefix.Length) != 0)
  135.                     {
  136.                         for (int i = 19; i >= 0; --i)
  137.                         {
  138.                             ++RND[i];
  139.                             //Roll over Hexadecimal number
  140.                             if (RND[i] == 0x47 /*G*/)
  141.                             {
  142.                                 RND[i] = 0x30 /*0*/;
  143.                             }
  144.                             else
  145.                             {
  146.                                 //Skip ASCII Gap between digits and letters
  147.                                 if (RND[i] == 0x3A /*:*/)
  148.                                 {
  149.                                     RND[i] = 0x41 /*A*/;
  150.                                 }
  151.                                 //Stop incrementing this number
  152.                                 break;
  153.                             }
  154.                         }
  155.                     }
  156.                     else
  157.                     {
  158.                         break;
  159.                     }
  160.                 }
  161.                 if (memcmp(Hash, Config.Prefix, Config.Prefix.Length) == 0)
  162.                 {
  163.                     FoundKey = RND;
  164.                     FoundHash = Hash;
  165.                 }
  166.             }
  167.         }
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement