Last active
February 19, 2019 23:12
-
-
Save mjs3339/d4484a763ad976af49202e814acf8b9b to your computer and use it in GitHub Desktop.
C# 32 Bit Hashing Algorithm ~No Collisions in the Int Bit Space
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Security.Cryptography; | |
/// <summary> | |
/// Pros: ~Very few to no Collisions within int bit space ~(2,147,483,647) | |
/// Cons: Maintains a TinyDictionary(byte[],byte[]), Decreased performance, non-deterministic across application or | |
/// method domains | |
/// Cannot Transform or reuse. | |
/// </summary> | |
public class Mapping64BitToHash32Bit : HashAlgorithm | |
{ | |
private readonly FNV1a64 hasher = new FNV1a64(); | |
public readonly TinyDictionary<byte[], byte[]> map = new TinyDictionary<byte[], byte[]>(101, new ArrayComparer()); | |
private byte[] h64; | |
public override int HashSize => 32; | |
public override void Initialize() | |
{ | |
} | |
/// <inheritdoc /> | |
/// <summary> | |
/// Compute the 64 Bit hash value and add it and the original array to the map to create a unique position within the | |
/// TinyDictionary. | |
/// </summary> | |
protected override void HashCore(byte[] bytes, int ibStart, int cbSize) | |
{ | |
h64 = hasher.ComputeHash(bytes, ibStart, cbSize); | |
map.Add(h64, bytes); | |
} | |
/// <inheritdoc /> | |
/// <summary> | |
/// Return the unique position within the TinyDictionary as the hash value (index). | |
/// </summary> | |
protected override byte[] HashFinal() | |
{ | |
HashValue = (byte[]) map.FindKeyIndex(h64).GetBytes().Clone(); | |
return HashValue; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment