diff --git a/README.md b/README.md index 507d6ab..4a46b24 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,6 @@ Nevertheless, the performance on Sandy Bridge @2.8GHz is as follows: * If only phrases of at most 3 words are allowed, then it takes 2.5 seconds to find and check all anagrams; all relevant hashes are solved in first 0.4 seconds; -* If phrases of 4 words are allowed as well, then it takes 70 seconds to find and check all anagrams; all hashes are solved in first 5 seconds; +* If phrases of 4 words are allowed as well, then it takes 40 seconds to find and check all anagrams; all hashes are solved in first 3 seconds; For comparison, certain other solutions available on GitHub seem to require 3 hours to find all 3-word anagrams (i.e. this solution is faster by a factor of 4000 in 3-word case). \ No newline at end of file diff --git a/WhiteRabbit/Program.cs b/WhiteRabbit/Program.cs index 11af2e3..b6634b5 100644 --- a/WhiteRabbit/Program.cs +++ b/WhiteRabbit/Program.cs @@ -5,8 +5,8 @@ using System.Diagnostics; using System.Linq; using System.Numerics; - using System.Security.Cryptography; using System.Text; + using Org.BouncyCastle.Crypto.Digests; /// /// Main class @@ -15,7 +15,7 @@ { const string SourcePhrase = "poultry outwits ants"; - const int MaxWordsInPhrase = 4; + const int MaxWordsInPhrase = 3; /// /// Main entry point @@ -33,7 +33,7 @@ "665e5bcb0c20062fe8abaaf4628bb154", }; - var expectedHashesAsVectors = new HashSet>(expectedHashes.Select(hash => new Vector(StringToByteArray(hash)))); + var expectedHashesAsVectors = expectedHashes.Select(hash => new Vector(StringToByteArray(hash))).ToArray(); foreach (var result in AddHashes(processor.GeneratePhrases(ReadInput()))) { @@ -58,16 +58,21 @@ private static IEnumerable>> AddHashes(IEnumerable input) { - using (MD5 hasher = MD5.Create()) + foreach (var line in input) { - foreach (var line in input) - { - var data = hasher.ComputeHash(line); - yield return Tuple.Create(line, new Vector(data)); - } + yield return Tuple.Create(line, ComputeHash(line)); } } + private static Vector ComputeHash(byte[] input) + { + var digest = new MD5Digest(); + digest.BlockUpdate(input, 0, input.Length); + byte[] hash = new byte[16]; + digest.DoFinal(hash, 0); + return new Vector(hash); + } + private static IEnumerable ReadInput() { string line; diff --git a/WhiteRabbit/WhiteRabbit.csproj b/WhiteRabbit/WhiteRabbit.csproj index 55cfa51..a0ac0c9 100644 --- a/WhiteRabbit/WhiteRabbit.csproj +++ b/WhiteRabbit/WhiteRabbit.csproj @@ -34,6 +34,10 @@ 4 + + ..\packages\BouncyCastle.1.8.1\lib\BouncyCastle.Crypto.dll + True + ..\packages\System.Collections.Immutable.1.3.1\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll diff --git a/WhiteRabbit/packages.config b/WhiteRabbit/packages.config index d38493d..d070340 100644 --- a/WhiteRabbit/packages.config +++ b/WhiteRabbit/packages.config @@ -1,5 +1,6 @@  + \ No newline at end of file