예제 #1
0
        //static public void FindCollision(IHashFunc hashFunc, byte[] ex)
        //{
        //    byte[] inp = new byte[ex.Length];
        //    string hash = "";
        //    string exHash = Encoding.ASCII.GetString(hashFunc.CalcHash(ex));
        //    ulong iterationsCounter = 0;

        //    Stopwatch stopwatch = new Stopwatch();

        //    stopwatch.Start();

        //    while (!hash.Equals(exHash))
        //    {
        //        iterationsCounter++;
        //        RandomizeByteArr(inp);

        //        hash = Encoding.ASCII.GetString(hashFunc.CalcHash(inp));
        //    }

        //    stopwatch.Stop();

        //    Console.WriteLine($"Found in {iterationsCounter} iterations \n Time spent {stopwatch.ElapsedMilliseconds} ms");
        //}

        //static public void RandomizeByteArr(byte[] inp)
        //{
        //    for (int i = 0; i < inp.Length; ++i)
        //        inp[i] = (byte)(random.Next(254)+1);
        //}

        static void ProofOfWork(IHashFunc hashFunc, string initStr, string matched)
        {
            string hash = "";
            ulong  iterationsCounter = 0;

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            while (!hash.StartsWith(matched))
            {
                iterationsCounter++;

                hash = Encoding.ASCII.GetString(hashFunc.CalcHash(Encoding.ASCII.GetBytes(initStr + iterationsCounter.ToString())));
            }

            stopwatch.Stop();

            Console.WriteLine($"Found in {iterationsCounter} iterations \n Time spent {stopwatch.ElapsedMilliseconds} ms");
        }