Exemplo n.º 1
0
        //
        // Public methods
        //
        #region Public methods

        // Constructor
        public PasswordProducerActivity(string cInitialPassword,
                                        PasswordQueue cPasswordQueue,
                                        CancellationToken cCancToken)
        {
            // Initialize members
            mPasswordQueue     = cPasswordQueue;
            mPasswordGenerator = new PasswordGenerator(cInitialPassword);
            mCancToken         = cCancToken;
        }
        //
        // Public methods
        //
        #region Public methods

        // Constructor
        public PasswordConsumerActivity(string cFileName,
                                        int cIndex,
                                        TaskCompletionSource <string> cPasswordRes,
                                        PasswordQueue cPasswordQueue,
                                        LogMessageQueue cMessageLogQueue)
        {
            // Initialize members
            mFileName        = cFileName;
            mPasswordRes     = cPasswordRes;
            mPasswordQueue   = cPasswordQueue;
            mPasswordChecker = new PasswordChecker(cFileName);
            mLogMessageQueue = cMessageLogQueue;
            mIndex           = cIndex;
        }
        //
        // Private methods
        //
        #region Private methods

        // Crack password
        private void DoCrackPassword(int cThreadsNum,
                                     string cInitialPassword)
        {
            // Create cancellation token
            using (CancellationTokenSource token_src = new CancellationTokenSource())
            {
                // Create task list
                List <Task> task_list = new List <Task>();

                // Create queues
                var log_msg_queue  = new LogMessageQueue();
                var password_queue = new PasswordQueue();

                // Create and add password producer
                var pwd_producer = new PasswordProducerActivity(cInitialPassword, password_queue, token_src.Token);
                task_list.Add(pwd_producer.Run());

                // Create and add log consumer
                var log_consumer = new LogConsumerActivity(cThreadsNum, log_msg_queue, token_src.Token);
                task_list.Add(log_consumer.Run());

                // Start stopwatch
                Stopwatch stop_watch = new Stopwatch();
                stop_watch.Start();

                // Create and add password consumers
                TaskCompletionSource <string> password_src = new TaskCompletionSource <string>();
                for (int i = 1; i <= cThreadsNum; i++)
                {
                    var pwd_consumer = new PasswordConsumerActivity(mFileName, i, password_src, password_queue, log_msg_queue);
                    task_list.Add(pwd_consumer.Run());
                }

                try
                {
                    // Wait for password to be found
                    Task <string> password_res = password_src.Task;
                    password_res.Wait();
                    // Log password
                    LogPassword(password_res.Result);
                }
                catch (Exception ex)
                {
                    // Re-throw exception if cannot be handled
                    if (!HandleException(ex))
                    {
                        throw;
                    }
                }
                finally
                {
                    // Stop stopwatch
                    stop_watch.Stop();
                    // Cancel the other tasks (log consumer and password producer)
                    token_src.Cancel();
                    // Wait for all to finish
                    Task.WaitAll(task_list.ToArray());
                    // Print elapsed time
                    Console.WriteLine(String.Format("Elapsed time: {0}", stop_watch.Elapsed));
                }
            }
        }