static void Main(string[] args) { // getting a string to calculate a hash function var inputString = ConsoleMessager.AskString(); Console.WriteLine("Task 1."); // Task 1. Getting hash from initial string value var mdFourInstance = GetHashFromString(inputString); // printing results to console ConsoleMessager.DemonstrateHashResult(inputString, mdFourInstance.ResultHashString); // Task 2. "The avalanche effect" Console.WriteLine("Task 2."); var inputStringWithOneBitChanged = ChangeOneBitInString(inputString); var mdFourInstanceWithOneBitChanged = GetHashFromString(inputStringWithOneBitChanged); // printing results to console ConsoleMessager.DemonstrateHashResult(inputStringWithOneBitChanged, mdFourInstanceWithOneBitChanged.ResultHashString); var numberOfDifferentBits = CalculateDifferentBits(mdFourInstance.ResultHashBytes, mdFourInstanceWithOneBitChanged.ResultHashBytes); // printing results to console ConsoleMessager.ShowDifferentBits(inputString, inputStringWithOneBitChanged, numberOfDifferentBits); // Task 3. finding a collision. Console.WriteLine("Task 3."); FindAndPrintCollision(); // Task 4. Finding prototype Console.WriteLine("Task 4."); FindAndPrintPrototype(); }
/// <summary> /// Task 4. Prototype /// </summary> private static void FindAndPrintPrototype() { Console.WriteLine("Enter a password:"******""; int N = 0; while (prototypeString == "") { N++; var randomString = GetRandomString(L); var mdFourRandomStringInstance = GetHashFromString(randomString); var randomHashFirstBitsList = GetBitsList(mdFourRandomStringInstance.ResultHashBytes, k); var randomHashFirstBitsString = Helper.PrintList(randomHashFirstBitsList); if (randomHashFirstBitsString == firstBitsString) { Console.WriteLine(String.Format("We found a string with the same {0} first bits in hash for your password '{1}'", k, inputPassword)); prototypeString = randomString; ConsoleMessager.PrintCollisionResults(mdFourInitialInstance.ResultHashString, inputPassword, mdFourRandomStringInstance.ResultHashString, randomString, randomHashFirstBitsString, N); } } }