Switched to BouncyCastle for MD5

plinq
Inga 🏳‍🌈 8 years ago
parent 2bb80c719a
commit 92d995ac79
  1. 2
      README.md
  2. 19
      WhiteRabbit/Program.cs
  3. 4
      WhiteRabbit/WhiteRabbit.csproj
  4. 1
      WhiteRabbit/packages.config

@ -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).

@ -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;
/// <summary>
/// Main class
@ -15,7 +15,7 @@
{
const string SourcePhrase = "poultry outwits ants";
const int MaxWordsInPhrase = 4;
const int MaxWordsInPhrase = 3;
/// <summary>
/// Main entry point
@ -33,7 +33,7 @@
"665e5bcb0c20062fe8abaaf4628bb154",
};
var expectedHashesAsVectors = new HashSet<Vector<byte>>(expectedHashes.Select(hash => new Vector<byte>(StringToByteArray(hash))));
var expectedHashesAsVectors = expectedHashes.Select(hash => new Vector<byte>(StringToByteArray(hash))).ToArray();
foreach (var result in AddHashes(processor.GeneratePhrases(ReadInput())))
{
@ -57,15 +57,20 @@
}
private static IEnumerable<Tuple<byte[], Vector<byte>>> AddHashes(IEnumerable<byte[]> input)
{
using (MD5 hasher = MD5.Create())
{
foreach (var line in input)
{
var data = hasher.ComputeHash(line);
yield return Tuple.Create(line, new Vector<byte>(data));
yield return Tuple.Create(line, ComputeHash(line));
}
}
private static Vector<byte> 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<byte>(hash);
}
private static IEnumerable<byte[]> ReadInput()

@ -34,6 +34,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="BouncyCastle.Crypto, Version=1.8.1.0, Culture=neutral, PublicKeyToken=0e99375e54769942">
<HintPath>..\packages\BouncyCastle.1.8.1\lib\BouncyCastle.Crypto.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Collections.Immutable, Version=1.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Collections.Immutable.1.3.1\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="BouncyCastle" version="1.8.1" targetFramework="net46" />
<package id="System.Collections.Immutable" version="1.3.1" targetFramework="net46" />
<package id="System.Numerics.Vectors" version="4.3.0" targetFramework="net46" />
</packages>
Loading…
Cancel
Save