From 3f54dbdd7c872fdd7b0762e47969fd03fecbf7dc Mon Sep 17 00:00:00 2001 From: Cory Gehr Date: Fri, 4 Dec 2020 15:17:47 -0800 Subject: [PATCH] Add files --- FNV1A.sln | 25 ++++++ FNV1A/Extensions.cs | 32 +++++++ FNV1A/FNV1A.csproj | 8 ++ FNV1A/Fnv1a128.cs | 28 ++++++ FNV1A/Fnv1aBase.cs | 93 +++++++++++++++++++ FNV1A/Program.cs | 130 +++++++++++++++++++++++++++ FNV1A/Properties/launchSettings.json | 8 ++ 7 files changed, 324 insertions(+) create mode 100644 FNV1A.sln create mode 100644 FNV1A/Extensions.cs create mode 100644 FNV1A/FNV1A.csproj create mode 100644 FNV1A/Fnv1a128.cs create mode 100644 FNV1A/Fnv1aBase.cs create mode 100644 FNV1A/Program.cs create mode 100644 FNV1A/Properties/launchSettings.json diff --git a/FNV1A.sln b/FNV1A.sln new file mode 100644 index 0000000..c077cce --- /dev/null +++ b/FNV1A.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30717.126 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FNV1A", "FNV1A\FNV1A.csproj", "{AACFEC31-0FD3-4292-A5D6-8B223A66801B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AACFEC31-0FD3-4292-A5D6-8B223A66801B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AACFEC31-0FD3-4292-A5D6-8B223A66801B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AACFEC31-0FD3-4292-A5D6-8B223A66801B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AACFEC31-0FD3-4292-A5D6-8B223A66801B}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {9A9BA56B-9400-4484-B150-33AFEA9F7519} + EndGlobalSection +EndGlobal diff --git a/FNV1A/Extensions.cs b/FNV1A/Extensions.cs new file mode 100644 index 0000000..f87fe55 --- /dev/null +++ b/FNV1A/Extensions.cs @@ -0,0 +1,32 @@ +namespace FNV1A +{ + using System; + + internal static class Extensions + { + /// + /// Adds a zero byte on to the end of the byte array. + /// + /// The bytes. + /// The new array with a zero byte on the end. + /// The array is multidimensional and contains more than + /// elements. + /// sourceArray and destinationArray have different ranks. + /// sourceArray and destinationArray are of incompatible + /// types. + /// At least one element in sourceArray cannot be cast to the + /// type of destinationArray. + /// sourceArray is null. -or- destinationArray is + /// null. + /// length is less than zero. + /// length is greater than the number of elements in sourceArray. + /// -or- length is greater than the number of elements in destinationArray. + internal static byte[] AddZero(this byte[] bytes) + { + byte[] temp = new byte[bytes.Length + 16]; + + Array.Copy(bytes, temp, bytes.Length); + return temp; + } + } +} diff --git a/FNV1A/FNV1A.csproj b/FNV1A/FNV1A.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/FNV1A/FNV1A.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/FNV1A/Fnv1a128.cs b/FNV1A/Fnv1a128.cs new file mode 100644 index 0000000..956c50a --- /dev/null +++ b/FNV1A/Fnv1a128.cs @@ -0,0 +1,28 @@ +namespace FNV1A +{ + using static System.Globalization.CultureInfo; + using static System.Globalization.NumberStyles; + using static System.Numerics.BigInteger; + + public sealed class Fnv1a128 : Fnv1aBase + { + /// + /// + /// Initializes a new instance of the class. + /// + /// style is not a + /// value. -or- style includes the + /// or flag along with another + /// value. + /// value is null. + /// value does not comply with the input pattern specified by + /// style. + public Fnv1a128() : base( + Parse("100000000000000000000000000000000", AllowHexSpecifier, InvariantCulture), + Parse("0000000001000000000000000000013B", AllowHexSpecifier, InvariantCulture), + Parse("6C62272E07BB014262B821756295C58D", AllowHexSpecifier, InvariantCulture), + 128) + { + } + } +} diff --git a/FNV1A/Fnv1aBase.cs b/FNV1A/Fnv1aBase.cs new file mode 100644 index 0000000..dfaffdf --- /dev/null +++ b/FNV1A/Fnv1aBase.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Numerics; +using System.Text; + +namespace FNV1A +{ + /// + /// Base class for FNV1A implementations + /// + public abstract class Fnv1aBase : System.Security.Cryptography.HashAlgorithm + { + /// + /// The "wrap-around" modulo value for keeping multiplication within the number of bits. + /// + private readonly BigInteger _modValue; + + /// + /// The prime. + /// + private readonly BigInteger _prime; + + /// + /// The non-zero offset basis. + /// + private readonly BigInteger _offsetBasis; + + /// + /// The hash. + /// + private BigInteger _hash; + + /// + /// + /// Initializes a new instance of the class. + /// + /// The "wrap-around" modulo value for keeping multiplication within the number of + /// bits. + /// The FNV-1a prime. + /// The FNV-1a offset basis. + /// The size, in bits, of the computed hash code. + // ReSharper disable once TooManyDependencies + protected Fnv1aBase(BigInteger modValue, BigInteger prime, BigInteger offsetBasis, int hashSizeValue) + { + this._modValue = modValue; + this._prime = prime; + this._offsetBasis = offsetBasis; + this.Init(); + this.HashSizeValue = hashSizeValue; + } + + /// + /// + /// Initializes an implementation of the class. + /// + public override sealed void Initialize() => this.Init(); + + /// + /// + /// When overridden in a derived class, routes data written to the object into the hash algorithm for computing + /// the hash. + /// + /// The input to compute the hash code for. + /// The offset into the byte array from which to begin using data. + /// The number of bytes in the byte array to use as data. + protected override void HashCore(byte[] array, int ibStart, int cbSize) + { + for (int i = ibStart; i < cbSize; i++) + { + unchecked + { + this._hash ^= array[i]; + this._hash = (this._hash * this._prime) % this._modValue; + } + } + } + + /// + /// + /// When overridden in a derived class, finalizes the hash computation after the last data is processed by the + /// cryptographic stream object. + /// + /// + /// The computed hash code. + /// + protected override byte[] HashFinal() => this._hash.ToByteArray(); + + /// + /// Initializes the hash for this instance. + /// + private void Init() => this._hash = this._offsetBasis; + } +} \ No newline at end of file diff --git a/FNV1A/Program.cs b/FNV1A/Program.cs new file mode 100644 index 0000000..72c7ede --- /dev/null +++ b/FNV1A/Program.cs @@ -0,0 +1,130 @@ +namespace FNV1A +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Numerics; + using System.Runtime.CompilerServices; + using System.Security.Cryptography; + using System.Text; + + class Program + { + static HashAlgorithm Algo = new Fnv1a128(); + + static void Main(string[] args) + { + if(args.Length > 3) + { + string hashPath = args[0]; + string wordPath = args[1]; + string outputPath = args[2]; + string remainingHashesPath = String.Empty; + + if(args.Length == 4) + { + remainingHashesPath = args[3]; + } + + try + { + // Must preserve order + string[] hashLines = System.IO.File.ReadAllLines(hashPath); + string[] outputLines = new string[hashLines.Length]; + + // initialize + for (int i = 0; i < outputLines.Length; i++) + { + outputLines[i] = String.Empty; + } + + using (StreamReader reader = new StreamReader(wordPath)) + { + string line; + int lineCount = 0; + while((line = reader.ReadLine()) != null) + { + string encodedWord = Fnv1a128s(line).ToLower(); + + // Check if encoding matches in password list + for(int i=0; i + /// Computes the FNV-1a 128-bit hash for the specified data. + /// + /// The data. + /// The FNV-1a 128-bit hash of the specified data. + // ReSharper disable once InconsistentNaming + private static string Fnv1a128s(string data) + { + using (HashAlgorithm alg = new Fnv1a128()) + { + string value = new BigInteger(alg.ComputeHash(Encoding.UTF8.GetBytes(data)).AddZero()).ToString("X32", System.Globalization.CultureInfo.InvariantCulture); + + return value.Substring(value.Length - 32); + } + } + } +} \ No newline at end of file diff --git a/FNV1A/Properties/launchSettings.json b/FNV1A/Properties/launchSettings.json new file mode 100644 index 0000000..22d82a4 --- /dev/null +++ b/FNV1A/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "FNV1A": { + "commandName": "Project", + "commandLineArgs": "\"C:\\Users\\cogehr\\OneDrive - Microsoft\\Desktop\\forescient\\remaining.txt\" \"C:\\Users\\cogehr\\OneDrive - Microsoft\\Desktop\\forescient\\words6.txt\"" + } + } +} \ No newline at end of file