Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- using System.IO;
- using System.Collections.Concurrent;
- using System.Runtime.InteropServices;
- using System.Security.Cryptography;
- namespace md5
- {
- class Program
- {
- static MFile[] SFiles=new MFile[256];
- static MD5 md5Hash = new MD5CryptoServiceProvider();
- static void Main(string[] args)
- {
- Stage1();
- Stage2();
- }
- static void Stage2()
- {
- long length = new System.IO.FileInfo(@"D:\md5test\00.s1").Length;
- TypeCast buf = new TypeCast();
- buf.Byte = new byte[length + 2048 * 1024];
- byte[] cnt = new byte[0x1000000];
- Int32[] offs = new Int32[0x1000000];
- byte[] wcnt = new byte[0x1000000];
- TypeCast oBuf = new TypeCast();
- UInt32[] keys = new UInt32[(length + 2048 * 1024) / 2];
- oBuf.Byte = new byte[(length + 2048 * 1024) / 2];
- var outFile = File.Create(@"D:\md5test\_result.bin");
- for (int f = 0; f <= 255; f++)
- {
- string path = @"D:\md5test\" + f.ToString("x2") + ".s1";
- Console.WriteLine(path);
- length = new System.IO.FileInfo(path).Length;
- using (var file = File.OpenRead(path))
- {
- file.Read(buf.Byte, 0, (int)length);
- }
- int max = (int)length / 8;
- Array.Clear(cnt, 0, cnt.Length);
- Array.Clear(wcnt, 0, wcnt.Length);
- for (int i = 0; i < max; i++)
- {
- buf.UInt32[i * 2 + 1] = ReverseBytes(buf.UInt32[i * 2 + 1]);
- cnt[buf.UInt32[i * 2 + 1] >> 8]++;
- }
- int sum = 0;
- int[] ccnt = new int[20];
- for (int i = 0; i < 0x1000000; i++)
- {
- offs[i] = sum;
- sum += cnt[i];
- }
- for (int i = 0; i < max; i++)
- {
- UInt32 ok = buf.UInt32[i * 2 + 1];
- UInt32 k = ok >> 8;
- UInt32 v = buf.UInt32[i * 2];
- int Base = offs[k];
- if (wcnt[k] == 0)
- {
- oBuf.UInt32[Base] = v; keys[Base] = ok;
- }
- else
- {
- int j=0,maxj= wcnt[k];
- while (
- j < maxj &&
- ( keys[Base + j] < ok ||
- ( keys[Base + j] == ok && CompareMD5(oBuf.UInt32[Base+j], v) < 0 )
- )
- ) j++;
- while (true)
- {
- UInt32 o_ok = keys[Base + j]; UInt32 o_v = oBuf.UInt32[Base + j];
- oBuf.UInt32[Base + j] = v; keys[Base + j] = ok;
- if (j >= maxj) break;
- v = o_v; ok = o_ok;
- j++;
- }
- }
- wcnt[k]++;
- }
- outFile.Write(oBuf.Byte, 0, (int)length/2);
- }
- outFile.Close();
- }
- public static UInt32 ReverseBytes(UInt32 value)
- {
- return (value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 |
- (value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24;
- }
- static int CompareMD5(UInt32 v1, UInt32 v2)
- {
- byte[] c1 = md5Hash.ComputeHash(BitConverter.GetBytes(v1));
- byte[] c2 = md5Hash.ComputeHash(BitConverter.GetBytes(v2));
- for (int i=0;i<16;i++)
- {
- if (c1[i] == c2[i]) continue;
- return (c1[i] < c2[i] ? -1 : 1);
- }
- if (v1 != v2) Console.Write("MD5 OF " + v1.ToString() + " and " + v2.ToString() + " IS EQUAL !!!");
- return 0;
- }
- static void Stage1()
- {
- CreateSFiles();
- Console.WriteLine("Stage1 started");
- UInt32 val = 0;
- do
- {
- byte[] hash = md5Hash.ComputeHash(BitConverter.GetBytes(val));
- SFiles[hash[0]].Add(val, BitConverter.ToUInt32(hash, 1));
- if ((val & 0xFFFF) == 0)
- {
- Console.WriteLine(val.ToString("x8"));
- Console.Out.Flush();
- }
- } while (++val != 0);
- for (int i = 0; i <= 255; i++)
- {
- SFiles[i].Dispose();
- SFiles[i] = null;
- }
- MFile.freeBuffers();
- }
- static void CreateSFiles()
- {
- for(int i=0;i<=255;i++)
- {
- Console.WriteLine("Open " + i.ToString());
- SFiles[i] = new MFile(i);
- }
- }
- }
- [StructLayout(LayoutKind.Explicit, Pack = 2)]
- public class TypeCast
- {
- [FieldOffset(0)]
- public int numberOfBytes;
- [FieldOffset(4)]
- public byte[] Byte;
- [FieldOffset(4)]
- public UInt32[] UInt32;
- }
- public class MFile : IDisposable
- {
- private const int maxBufSize = 262144;
- private FileStream file;
- private int num;
- private TypeCast buf;
- private int cur; // Текущая позиция в буфере
- private static ConcurrentBag<TypeCast> freePoll = new ConcurrentBag<TypeCast>();
- private static Task saveTask=null;
- public MFile(int Num)
- {
- num = Num;
- file=File.Create(@"D:\md5test\" + num.ToString("x2") + ".s1");
- cur = 0;
- CreateBuf();
- }
- private void CreateBuf()
- {
- buf = new TypeCast();
- buf.Byte = new byte[maxBufSize*4];
- }
- public void Add(UInt32 val, UInt32 hash)
- {
- buf.UInt32[cur++] = val; buf.UInt32[cur++] = hash;
- if (cur == maxBufSize) saveTask=saveBuf(maxBufSize);
- return;
- }
- public async Task saveBuf(int len=0)
- {
- saveTask?.Wait();
- if (len == 0) len = cur;
- TypeCast oldBuf;
- oldBuf = buf;
- cur = 0;
- if (!freePoll.TryTake(out buf)) CreateBuf();
- await file.WriteAsync(oldBuf.Byte, 0, len * 4);
- freePoll.Add(oldBuf);
- saveTask = null;
- }
- public void Dispose()
- {
- if (cur != 0) saveTask=saveBuf();
- saveTask?.Wait();
- file.Close();
- }
- public static void freeBuffers()
- {
- TypeCast buf;
- while (freePoll.TryTake(out buf)) ;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement