Advertisement
Guest User

Untitled

a guest
Jan 10th, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.04 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.Threading;
  7. using System.IO;
  8. using System.Collections.Concurrent;
  9. using System.Runtime.InteropServices;
  10. using System.Security.Cryptography;
  11.  
  12. namespace md5
  13. {
  14.     class Program
  15.     {
  16.         static MFile[] SFiles=new MFile[256];
  17.         static MD5 md5Hash = new MD5CryptoServiceProvider();
  18.         static void Main(string[] args)
  19.         {
  20.             Stage1();
  21.             Stage2();
  22.         }
  23.         static void Stage2()
  24.         {
  25.             long length = new System.IO.FileInfo(@"D:\md5test\00.s1").Length;
  26.             TypeCast buf = new TypeCast();
  27.             buf.Byte = new byte[length + 2048 * 1024];
  28.             byte[] cnt = new byte[0x1000000];
  29.             Int32[] offs = new Int32[0x1000000];
  30.             byte[] wcnt = new byte[0x1000000];
  31.             TypeCast oBuf = new TypeCast();
  32.             UInt32[] keys = new UInt32[(length + 2048 * 1024) / 2];
  33.             oBuf.Byte = new byte[(length + 2048 * 1024) / 2];
  34.             var outFile = File.Create(@"D:\md5test\_result.bin");
  35.             for (int f = 0; f <= 255; f++)
  36.             {
  37.                 string path = @"D:\md5test\" + f.ToString("x2") + ".s1";
  38.                 Console.WriteLine(path);
  39.                 length = new System.IO.FileInfo(path).Length;
  40.                 using (var file = File.OpenRead(path))
  41.                 {
  42.                     file.Read(buf.Byte, 0, (int)length);
  43.                 }
  44.                 int max = (int)length / 8;
  45.                 Array.Clear(cnt, 0, cnt.Length);
  46.                 Array.Clear(wcnt, 0, wcnt.Length);
  47.                 for (int i = 0; i < max; i++)
  48.                 {
  49.                     buf.UInt32[i * 2 + 1] = ReverseBytes(buf.UInt32[i * 2 + 1]);
  50.                     cnt[buf.UInt32[i * 2 + 1] >> 8]++;
  51.                 }
  52.                 int sum = 0;
  53.                 int[] ccnt = new int[20];
  54.                 for (int i = 0; i < 0x1000000; i++)
  55.                 {
  56.                     offs[i] = sum;
  57.                     sum += cnt[i];
  58.                 }
  59.                 for (int i = 0; i < max; i++)
  60.                 {
  61.                     UInt32 ok = buf.UInt32[i * 2 + 1];
  62.                     UInt32 k = ok >> 8;
  63.                     UInt32 v = buf.UInt32[i * 2];
  64.                     int Base = offs[k];
  65.                     if (wcnt[k] == 0)
  66.                     {
  67.                         oBuf.UInt32[Base] = v; keys[Base] = ok;
  68.                     }
  69.                     else
  70.                     {
  71.                         int j=0,maxj= wcnt[k];
  72.                         while (
  73.                                 j < maxj &&
  74.                                 ( keys[Base + j] < ok ||
  75.                                   ( keys[Base + j] == ok && CompareMD5(oBuf.UInt32[Base+j], v) < 0 )
  76.                                 )
  77.                               ) j++;
  78.                         while (true)
  79.                         {
  80.                             UInt32 o_ok = keys[Base + j]; UInt32 o_v = oBuf.UInt32[Base + j];
  81.                             oBuf.UInt32[Base + j] = v; keys[Base + j] = ok;
  82.                             if (j >= maxj) break;
  83.                             v = o_v; ok = o_ok;
  84.                             j++;
  85.                         }
  86.                     }
  87.                     wcnt[k]++;
  88.                 }
  89.                 outFile.Write(oBuf.Byte, 0, (int)length/2);
  90.             }
  91.             outFile.Close();
  92.         }
  93.         public static UInt32 ReverseBytes(UInt32 value)
  94.         {
  95.             return (value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 |
  96.                 (value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24;
  97.         }
  98.         static int CompareMD5(UInt32 v1, UInt32 v2)
  99.         {
  100.             byte[] c1 = md5Hash.ComputeHash(BitConverter.GetBytes(v1));
  101.             byte[] c2 = md5Hash.ComputeHash(BitConverter.GetBytes(v2));
  102.             for (int i=0;i<16;i++)
  103.             {
  104.                 if (c1[i] == c2[i]) continue;
  105.                 return (c1[i] < c2[i] ? -1 : 1);
  106.             }
  107.             if (v1 != v2) Console.Write("MD5 OF " + v1.ToString() + " and " + v2.ToString() + " IS EQUAL !!!");
  108.             return 0;
  109.         }
  110.         static void Stage1()
  111.         {
  112.             CreateSFiles();
  113.             Console.WriteLine("Stage1 started");
  114.             UInt32 val = 0;
  115.             do
  116.             {
  117.                 byte[] hash = md5Hash.ComputeHash(BitConverter.GetBytes(val));
  118.                 SFiles[hash[0]].Add(val, BitConverter.ToUInt32(hash, 1));
  119.                 if ((val & 0xFFFF) == 0)
  120.                 {
  121.                     Console.WriteLine(val.ToString("x8"));
  122.                     Console.Out.Flush();
  123.                 }
  124.             } while (++val != 0);
  125.             for (int i = 0; i <= 255; i++)
  126.             {
  127.                 SFiles[i].Dispose();
  128.                 SFiles[i] = null;
  129.             }
  130.             MFile.freeBuffers();
  131.         }
  132.         static void CreateSFiles()
  133.         {
  134.             for(int i=0;i<=255;i++)
  135.             {
  136.                 Console.WriteLine("Open " + i.ToString());
  137.                 SFiles[i] = new MFile(i);
  138.             }
  139.         }
  140.     }
  141.     [StructLayout(LayoutKind.Explicit, Pack = 2)]
  142.     public class TypeCast
  143.     {
  144.         [FieldOffset(0)]
  145.         public int numberOfBytes;
  146.         [FieldOffset(4)]
  147.         public byte[] Byte;
  148.         [FieldOffset(4)]
  149.         public UInt32[] UInt32;
  150.     }
  151.     public class MFile : IDisposable
  152.     {
  153.         private const int maxBufSize = 262144;
  154.         private FileStream file;
  155.         private int num;
  156.         private TypeCast buf;
  157.         private int cur; // Текущая позиция в буфере
  158.         private static ConcurrentBag<TypeCast> freePoll =  new ConcurrentBag<TypeCast>();
  159.         private static Task saveTask=null;
  160.         public MFile(int Num)
  161.         {
  162.             num = Num;
  163.             file=File.Create(@"D:\md5test\" + num.ToString("x2") + ".s1");
  164.             cur = 0;
  165.             CreateBuf();
  166.         }
  167.         private void CreateBuf()
  168.         {
  169.             buf = new TypeCast();
  170.             buf.Byte = new byte[maxBufSize*4];
  171.         }
  172.         public void Add(UInt32 val, UInt32 hash)
  173.         {
  174.             buf.UInt32[cur++] = val; buf.UInt32[cur++] = hash;
  175.             if (cur == maxBufSize) saveTask=saveBuf(maxBufSize);
  176.             return;
  177.         }
  178.         public async Task saveBuf(int len=0)
  179.         {
  180.             saveTask?.Wait();
  181.             if (len == 0) len = cur;
  182.             TypeCast oldBuf;
  183.             oldBuf = buf;
  184.             cur = 0;
  185.             if (!freePoll.TryTake(out buf)) CreateBuf();
  186.             await file.WriteAsync(oldBuf.Byte, 0, len * 4);
  187.             freePoll.Add(oldBuf);
  188.             saveTask = null;
  189.         }
  190.         public void Dispose()
  191.         {
  192.             if (cur != 0) saveTask=saveBuf();
  193.             saveTask?.Wait();
  194.             file.Close();
  195.         }
  196.         public static void freeBuffers()
  197.         {
  198.             TypeCast buf;
  199.             while (freePoll.TryTake(out buf)) ;
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement