/// <summary> /// Task 3. Collision /// </summary> private static void FindAndPrintCollision() { // asking user how many bits (counting from the beginning of initial message) he wants to be the same var k = ConsoleMessager.AskHashLength(); // asking length for future string value generating var L = ConsoleMessager.AskStringLength(); // a dictionary where key is a bits piece and value is a random generated string var kBitsStringHashDict = new Dictionary <string, string>(); while (true) { var randString = GetRandomString(L); var mdFourInstance = GetHashFromString(randString); var firstBitsList = GetBitsList(mdFourInstance.ResultHashBytes, k); var firstBitsString = Helper.PrintList(firstBitsList); // when we try to add the same key an exception is going to be raised. // that means a collision has been found. try { kBitsStringHashDict.Add(firstBitsString, randString); } catch (ArgumentException e) { var firstHash = GetHashFromString(kBitsStringHashDict[firstBitsString]).ResultHashString; var secondHash = mdFourInstance.ResultHashString; var firstRandomString = kBitsStringHashDict[firstBitsString]; var secondRandomString = randString; var N = kBitsStringHashDict.Count; ConsoleMessager.PrintCollisionResults(firstHash, firstRandomString, secondHash, secondRandomString, firstBitsString, N); break; } } }
/// <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); } } }