Advertisement
mquinlan

MOSTest

Sep 16th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.90 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5.  
  6. namespace MOSTest
  7. {
  8.     class MainClass
  9.     {
  10.         /// <summary>
  11.         /// Random number generator. Only used to generate a test password.
  12.         /// </summary>
  13.         public static Random Random = new Random();
  14.  
  15.         /// <summary>
  16.         /// Password to ifnd
  17.         /// </summary>
  18.         public static string Password;
  19.  
  20.         /// <summary>
  21.         /// Legal password characters
  22.         /// </summary>
  23.         public static string LegalCharacters =
  24.             "abcdefghijklmnopqrstuvwxyz" +
  25.             "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
  26.             "0123456789" +
  27.             "~`!@#$%^&*()_+-=";
  28.  
  29.         /// <summary>
  30.         /// Test driver
  31.         /// </summary>
  32.         /// <param name="args"></param>
  33.         public static void Main(string[] args)
  34.         {
  35.             // Generate a test password
  36.             Password = GenerateRandomPassword(4);
  37.             Console.WriteLine("Passowrd is " + Password);
  38.  
  39.             // Find the password using the Task Parallel Library.
  40.             var stopwatch = Stopwatch.StartNew();
  41.             var word = ParallelTestAllWords("");
  42.             stopwatch.Stop();
  43.             if (word != null) Console.WriteLine("Parallel: Answer is " + word + "; Elapsed = " + stopwatch.Elapsed);
  44.             else Console.WriteLine("Parallel: No answer found");
  45.  
  46.             // Find the password using a sequential foreach
  47.             stopwatch = Stopwatch.StartNew();
  48.             word = SequentialTestAllWords("");
  49.             stopwatch.Stop();
  50.             if (word != null) Console.WriteLine("Sequential: Answer is " + word + "; Elapsed = " + stopwatch.Elapsed);
  51.             else Console.WriteLine("Sequential: No answer found");
  52.         }
  53.  
  54.         /// <summary>
  55.         /// Generate a random password for testing
  56.         /// </summary>
  57.         /// <param name="length">Length of the password</param>
  58.         /// <returns>Test password</returns>
  59.         private static string GenerateRandomPassword(int length)
  60.         {
  61.             var buf = new StringBuilder(length);
  62.             for (var i = 0; i < length; i++)
  63.             {
  64.                 buf.Append(LegalCharacters[Random.Next(LegalCharacters.Length)]);
  65.             }
  66.             return buf.ToString();
  67.         }
  68.  
  69.         /// <summary>
  70.         /// Use the TPL to find the password
  71.         /// </summary>
  72.         /// <param name="guess">Current guess</param>
  73.         /// <returns>The password that was found, or null if not found</returns>
  74.         private static string ParallelTestAllWords(string guess)
  75.         {
  76.             // If the guess is the correct length, see if it is the correct password
  77.             if (guess.Length == Password.Length)
  78.             {
  79.                 // If it is the correct password, return it
  80.                 if (string.Equals(guess, Password)) return guess;
  81.  
  82.                 // Otherwise indicate not found by returning null
  83.                 return null;
  84.             }
  85.  
  86.             // The guess isn't long enough. Try all the words beginning
  87.             // with the guess.
  88.             string result = null;
  89.             Parallel.ForEach(LegalCharacters, (ch, state) =>
  90.             {
  91.                 // Check the guess extended with one of the legal characters
  92.                 var word = ParallelTestAllWords(guess + ch);
  93.  
  94.                 // If this is the password, return it and stop the loop
  95.                 if (word != null)
  96.                 {
  97.                     result = word;
  98.                     state.Break();
  99.                 }
  100.             });
  101.  
  102.             // Return the password if found or null if not found
  103.             return result;
  104.         }
  105.  
  106.         /// <summary>
  107.         /// Use a foreach loop to find the password
  108.         /// </summary>
  109.         /// <param name="guess">Current guess</param>
  110.         /// <returns>The password that was found, or null if not found</returns>
  111.         private static string SequentialTestAllWords(string guess)
  112.         {
  113.             // If the guess is the correct length, see if it is the correct password
  114.             if (guess.Length == Password.Length)
  115.             {
  116.                 // If it is the correct password, return it
  117.                 if (string.Equals(guess, Password)) return guess;
  118.  
  119.                 // Otherwise indicate not found by returning null
  120.                 return null;
  121.             }
  122.  
  123.             // The guess isn't long enough. Try all the words beginning
  124.             // with the guess.
  125.             foreach (var ch in LegalCharacters)
  126.             {
  127.                 // Check the guess extended with one of the legal characters
  128.                 var word = SequentialTestAllWords(guess + ch);
  129.  
  130.                 // If this is the password, return it and stop the loop
  131.                 if (word != null) return word;
  132.             }
  133.  
  134.             // Password not found.
  135.             return null;
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement