Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Text;
- using System.Threading.Tasks;
- namespace MOSTest
- {
- class MainClass
- {
- /// <summary>
- /// Random number generator. Only used to generate a test password.
- /// </summary>
- public static Random Random = new Random();
- /// <summary>
- /// Password to ifnd
- /// </summary>
- public static string Password;
- /// <summary>
- /// Legal password characters
- /// </summary>
- public static string LegalCharacters =
- "abcdefghijklmnopqrstuvwxyz" +
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
- "0123456789" +
- "~`!@#$%^&*()_+-=";
- /// <summary>
- /// Test driver
- /// </summary>
- /// <param name="args"></param>
- public static void Main(string[] args)
- {
- // Generate a test password
- Password = GenerateRandomPassword(4);
- Console.WriteLine("Passowrd is " + Password);
- // Find the password using the Task Parallel Library.
- var stopwatch = Stopwatch.StartNew();
- var word = ParallelTestAllWords("");
- stopwatch.Stop();
- if (word != null) Console.WriteLine("Parallel: Answer is " + word + "; Elapsed = " + stopwatch.Elapsed);
- else Console.WriteLine("Parallel: No answer found");
- // Find the password using a sequential foreach
- stopwatch = Stopwatch.StartNew();
- word = SequentialTestAllWords("");
- stopwatch.Stop();
- if (word != null) Console.WriteLine("Sequential: Answer is " + word + "; Elapsed = " + stopwatch.Elapsed);
- else Console.WriteLine("Sequential: No answer found");
- }
- /// <summary>
- /// Generate a random password for testing
- /// </summary>
- /// <param name="length">Length of the password</param>
- /// <returns>Test password</returns>
- private static string GenerateRandomPassword(int length)
- {
- var buf = new StringBuilder(length);
- for (var i = 0; i < length; i++)
- {
- buf.Append(LegalCharacters[Random.Next(LegalCharacters.Length)]);
- }
- return buf.ToString();
- }
- /// <summary>
- /// Use the TPL to find the password
- /// </summary>
- /// <param name="guess">Current guess</param>
- /// <returns>The password that was found, or null if not found</returns>
- private static string ParallelTestAllWords(string guess)
- {
- // If the guess is the correct length, see if it is the correct password
- if (guess.Length == Password.Length)
- {
- // If it is the correct password, return it
- if (string.Equals(guess, Password)) return guess;
- // Otherwise indicate not found by returning null
- return null;
- }
- // The guess isn't long enough. Try all the words beginning
- // with the guess.
- string result = null;
- Parallel.ForEach(LegalCharacters, (ch, state) =>
- {
- // Check the guess extended with one of the legal characters
- var word = ParallelTestAllWords(guess + ch);
- // If this is the password, return it and stop the loop
- if (word != null)
- {
- result = word;
- state.Break();
- }
- });
- // Return the password if found or null if not found
- return result;
- }
- /// <summary>
- /// Use a foreach loop to find the password
- /// </summary>
- /// <param name="guess">Current guess</param>
- /// <returns>The password that was found, or null if not found</returns>
- private static string SequentialTestAllWords(string guess)
- {
- // If the guess is the correct length, see if it is the correct password
- if (guess.Length == Password.Length)
- {
- // If it is the correct password, return it
- if (string.Equals(guess, Password)) return guess;
- // Otherwise indicate not found by returning null
- return null;
- }
- // The guess isn't long enough. Try all the words beginning
- // with the guess.
- foreach (var ch in LegalCharacters)
- {
- // Check the guess extended with one of the legal characters
- var word = SequentialTestAllWords(guess + ch);
- // If this is the password, return it and stop the loop
- if (word != null) return word;
- }
- // Password not found.
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement