public HasherBackgroundWorker(RDHash.RDHasher hasher, long iterations, IDataSet hashmap, int blockSize, int seed) { HashMap = hashmap; BlockSize = blockSize; Iterations = iterations; Hasher = hasher; Rnd = new Random(seed); ThisThread = new Thread(DoThread); ThisThread.Name = "BackgroundThread_worker"; ThisThread.Priority = ThreadPriority.BelowNormal; ThisThread.IsBackground = true; }
static void Main(string[] args) { Console.WriteLine("RDHash Library static collision check tool"); Console.WriteLine("Blocksize is {0} bytes long.", BLOCK_SIZE.ToString("N0")); RDHash.RDHasher hasher = new RDHash.RDHasher(); hasher.MinHashSize = 14; CollisionFileStream = new FileStream(".\\collisions.log", FileMode.Append, FileAccess.Write, FileShare.Read); CollisionFileWriter = new StreamWriter(CollisionFileStream, Encoding.ASCII, 4096, true); double n = Math.Pow(hasher.EncodeWheel.GlyphCount, hasher.MinHashSize); double k = ITERATIONS * REPETITIONS; double exponent = (-k * (k - 1)) / (2 * n); double chance = 1 - Math.Pow(Math.E, exponent); Console.WriteLine("Using {0} total CPU threads.", TOTAL_THREADS.ToString("N0")); Console.WriteLine("Clash chance: {0} %", (chance * 100).ToString("N5")); Console.WriteLine(); Console.WriteLine(); Random rng = new Random(); for (int repetition = 1; repetition <= REPETITIONS; repetition++) { CurrentRepetition = repetition; LastLogAddress = Console.CursorTop; Stopwatch sw = new Stopwatch(); UpdateStats(null, true); sw.Start(); IDataSet hashes; if (USE_HSD) { hashes = new DoubleHashedDataSetWrapper(ITERATIONS, true); } else { hashes = new HashSetWrapper(ITERATIONS); } Threads = new List <HasherBackgroundWorker>(); long iterations = ITERATIONS; while (iterations > 0) { long slice = (ITERATIONS / TOTAL_THREADS); var worker = new HasherBackgroundWorker(hasher, Math.Min(slice, iterations), hashes, BLOCK_SIZE, rng.Next(int.MinValue, int.MaxValue)); worker.PrintMessage += OnThreadPrintMessage; worker.PrintCollisionMessage += OnThreadPrintCollisionMessage; Threads.Add(worker); iterations -= slice; } for (int i = 0; i < Threads.Count; i++) { Threads[i].Start(); } while (IsRunning) { Thread.Sleep(UI_UPDATE_DELAY); UpdateStats(Threads[0]); } sw.Stop(); PrintMessage(string.Format("Pass {0} of {1}. {2} hashes calculated. {3} collisions found. {4}.{5} time spent.", repetition.ToString("N0"), REPETITIONS.ToString("N0"), hashes.Count.ToString("N0"), TotalCollisions.ToString("N0"), sw.Elapsed.ToString("hh\\:mm\\:ss"), sw.Elapsed.Milliseconds.ToString())); Threads.Clear(); hashes.Clear(); GC.Collect(); } CollisionFileWriter.Flush(); CollisionFileWriter.Dispose(); CollisionFileStream.Flush(); CollisionFileStream.Dispose(); PrintMessage(""); PrintMessage("Operation completed."); PrintMessage("- Press any key to exit -"); Console.ReadKey(); }