示例#1
0
        public void TestOneSecondPackAndSolveIncremental()
        {
            string plainTextDecoded = "HELLO WORLD";

            byte[] plainTextPayload = Encoding.UTF8.GetBytes(plainTextDecoded);

            ushort difficulty = Calculator.GetBitsForPayloadDecodeTime(plainTextPayload, TimeSpan.FromSeconds(1), EstimationAccuracy.VeryLow);

            CashPack pack = Packer.Pack(plainTextPayload, difficulty);

            ProgressPack progressPack = ProgressPack.Create(pack);

            byte[] result;

            while (!Solver.SolveIncremental(1000, progressPack, out result))
            {
                ;
            }

            string solvedDecoded = Encoding.UTF8.GetString(result);

            Assert.AreEqual(plainTextDecoded, solvedDecoded);
        }
示例#2
0
        public static void Main(string[] args)
        {
            // Alloc AES key
            byte[] key = new byte[32];

            // Generate AES ey
            using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
            {
                rng.GetBytes(key);
            }

            // Get the amount of seconds for each difficulty
            ulong[] res = Calculator.CalculateTimeForDifficultyLookup(key);

            // Print all times
            for (int i = 0; i < res.Length; i++)
            {
                // Print time for i difficulty
                Console.WriteLine("Difficulty " + i + " would take an average of " + res[i] + " seconds to unpack");
            }


            // Encode the payload as bytes.
            byte[] decryptedPayload = Encoding.UTF8.GetBytes(Lipsum);

            // Calculate the difficulty for the current machine. Takes ~10 seconds.
            Console.WriteLine("Calculating difficulty for an average solve time of 1 minute...");
            ushort difficulty = Calculator.GetBitsForPayloadDecodeTime(decryptedPayload, TimeSpan.FromMinutes(1));

            Console.WriteLine("Difficulty required is " + difficulty);


            // Packs the payload. Very fast operation.
            Console.WriteLine("Packing payload with " + difficulty + " difficulty...");
            CashPack pack = Packer.Pack(decryptedPayload, difficulty);

            Console.WriteLine("Packing completed!");


            // Create measurement watch.
            Stopwatch oneGoSolveTime = new Stopwatch();

            oneGoSolveTime.Start();

            {
                // Solves the CashPack in one go.
                Console.WriteLine("Solving HashPack in one go...");
                byte[] result = Solver.Solve(pack);
                Console.WriteLine("Solved CashPack successfully in " + oneGoSolveTime.ElapsedMilliseconds + " ms!");
            }

            // Stop watch.
            oneGoSolveTime.Stop();

            // Creates a CashPack container with progress.
            ProgressPack progressPack = ProgressPack.Create(pack);

            // Create measurements for the incremental solve.
            int       increments       = 0;
            Stopwatch incrementalWatch = new Stopwatch();

            incrementalWatch.Start();

            {
                // Solves the CashPack in an incremental manner. It will attempt to solve it 10_000 times, then return.
                Console.WriteLine("Solving CashPack in iterations of 10.000...");
                while (!Solver.SolveIncremental(10_000, progressPack, out byte[] result))
                {
                    // Here you can save the progressPack to disk.
                    // You can always resume the incremental solving later by reconstructing the progressPack and calling SolveIncremental on it.
                    increments++;
                }
                Console.WriteLine("Solved CashPack incrementally successfully after " + increments + " increments in " + incrementalWatch.ElapsedMilliseconds + " ms!");
            }

            // Stop watch.
            incrementalWatch.Stop();
        }