Skip to content

Commit

Permalink
Add files
Browse files Browse the repository at this point in the history
  • Loading branch information
Cory Gehr committed Dec 4, 2020
1 parent 0c04d40 commit 3f54dbd
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 0 deletions.
25 changes: 25 additions & 0 deletions FNV1A.sln
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions FNV1A/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace FNV1A
{
using System;

internal static class Extensions
{
/// <summary>
/// Adds a zero byte on to the end of the byte array.
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <returns>The new array with a zero byte on the end.</returns>
/// <exception cref="OverflowException">The array is multidimensional and contains more than
/// <see cref="int.MaxValue"></see> elements.</exception>
/// <exception cref="RankException">sourceArray and destinationArray have different ranks.</exception>
/// <exception cref="ArrayTypeMismatchException">sourceArray and destinationArray are of incompatible
/// types.</exception>
/// <exception cref="InvalidCastException">At least one element in sourceArray cannot be cast to the
/// type of destinationArray.</exception>
/// <exception cref="ArgumentNullException">sourceArray is null. -or- destinationArray is
/// null.</exception>
/// <exception cref="ArgumentOutOfRangeException">length is less than zero.</exception>
/// <exception cref="ArgumentException">length is greater than the number of elements in sourceArray.
/// -or- length is greater than the number of elements in destinationArray.</exception>
internal static byte[] AddZero(this byte[] bytes)
{
byte[] temp = new byte[bytes.Length + 16];

Array.Copy(bytes, temp, bytes.Length);
return temp;
}
}
}
8 changes: 8 additions & 0 deletions FNV1A/FNV1A.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>
28 changes: 28 additions & 0 deletions FNV1A/Fnv1a128.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="Fnv1a128"/> class.
/// </summary>
/// <exception cref="System.ArgumentException">style is not a
/// <see cref="System.Globalization.NumberStyles"></see> value. -or- style includes the
/// <see cref="AllowHexSpecifier"></see> or <see cref="HexNumber"></see> flag along with another
/// value.</exception>
/// <exception cref="System.ArgumentNullException">value is null.</exception>
/// <exception cref="System.FormatException">value does not comply with the input pattern specified by
/// style.</exception>
public Fnv1a128() : base(
Parse("100000000000000000000000000000000", AllowHexSpecifier, InvariantCulture),
Parse("0000000001000000000000000000013B", AllowHexSpecifier, InvariantCulture),
Parse("6C62272E07BB014262B821756295C58D", AllowHexSpecifier, InvariantCulture),
128)
{
}
}
}
93 changes: 93 additions & 0 deletions FNV1A/Fnv1aBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;

namespace FNV1A
{
/// <summary>
/// Base class for FNV1A implementations
/// </summary>
public abstract class Fnv1aBase : System.Security.Cryptography.HashAlgorithm
{
/// <summary>
/// The "wrap-around" modulo value for keeping multiplication within the number of bits.
/// </summary>
private readonly BigInteger _modValue;

/// <summary>
/// The prime.
/// </summary>
private readonly BigInteger _prime;

/// <summary>
/// The non-zero offset basis.
/// </summary>
private readonly BigInteger _offsetBasis;

/// <summary>
/// The hash.
/// </summary>
private BigInteger _hash;

/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="Fnv1aBigBase"/> class.
/// </summary>
/// <param name="modValue">The "wrap-around" modulo value for keeping multiplication within the number of
/// bits.</param>
/// <param name="prime">The FNV-1a prime.</param>
/// <param name="offsetBasis">The FNV-1a offset basis.</param>
/// <param name="hashSizeValue">The size, in bits, of the computed hash code.</param>
// 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;
}

/// <inheritdoc />
/// <summary>
/// Initializes an implementation of the <see cref="T:System.Security.Cryptography.HashAlgorithm" /> class.
/// </summary>
public override sealed void Initialize() => this.Init();

/// <inheritdoc />
/// <summary>
/// When overridden in a derived class, routes data written to the object into the hash algorithm for computing
/// the hash.
/// </summary>
/// <param name="array">The input to compute the hash code for.</param>
/// <param name="ibStart">The offset into the byte array from which to begin using data.</param>
/// <param name="cbSize">The number of bytes in the byte array to use as data.</param>
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;
}
}
}

/// <inheritdoc />
/// <summary>
/// When overridden in a derived class, finalizes the hash computation after the last data is processed by the
/// cryptographic stream object.
/// </summary>
/// <returns>
/// The computed hash code.
/// </returns>
protected override byte[] HashFinal() => this._hash.ToByteArray();

/// <summary>
/// Initializes the hash for this instance.
/// </summary>
private void Init() => this._hash = this._offsetBasis;
}
}
130 changes: 130 additions & 0 deletions FNV1A/Program.cs
Original file line number Diff line number Diff line change
@@ -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<hashLines.Length; i++)
{
string hash = hashLines[i];
if(encodedWord == hash)
{
outputLines[i] = line;
}
}

lineCount++;

if(lineCount % 100 == 0)
{
Console.WriteLine(lineCount + " words processed.");
}
}
}

// Dump results to file
int writtenCount = 0;
using (System.IO.StreamWriter file = new System.IO.StreamWriter(outputPath))
{
for (int i = 0; i < outputLines.Length; i++)
{
if (!String.IsNullOrEmpty(outputLines[i]))
{
file.WriteLine(String.Format("{0}: {1}", hashLines[i], outputLines[i]));
writtenCount++;
}
}
}

Console.WriteLine("Wrote " + writtenCount + " results.");

// Save remaining hashes to another file
if(!String.IsNullOrEmpty(remainingHashesPath))
{
int remainingCount = 0;
using (System.IO.StreamWriter file = new System.IO.StreamWriter(remainingHashesPath))
{
for (int i = 0; i < outputLines.Length; i++)
{
if (String.IsNullOrEmpty(outputLines[i]))
{
file.WriteLine(hashLines[i]);
remainingCount++;
}
}
}

Console.WriteLine(remainingCount + " remain.");
}
}
catch(Exception ex)
{
Console.WriteLine("Failed: " + ex.Message);
}
}
else
{
Console.WriteLine("Provide a path to the input hashes (0), wordlist (1), output path (2), and optionally a file to store uncracked hashes (3).");
}
}

/// <summary>
/// Computes the FNV-1a 128-bit hash for the specified data.
/// </summary>
/// <param name="data">The data.</param>
/// <returns>The FNV-1a 128-bit hash of the specified data.</returns>
// 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);
}
}
}
}
8 changes: 8 additions & 0 deletions FNV1A/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -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\""
}
}
}

0 comments on commit 3f54dbd

Please sign in to comment.