Skip to content

Instantly share code, notes, and snippets.

@mjs3339
Created January 12, 2021 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjs3339/f412ff82136ff2b36b9b18f24ed99939 to your computer and use it in GitHub Desktop.
Save mjs3339/f412ff82136ff2b36b9b18f24ed99939 to your computer and use it in GitHub Desktop.
Mapping 256Bit Hash to 32Bit Indexed Hash
using System.Security.Cryptography;
public class Mapping256BitTo32BitHash : HashAlgorithm
{
private readonly SHA256Managed hasher = new SHA256Managed();
public readonly TinyDictionary<byte[], byte[]> map = new TinyDictionary<byte[], byte[]>(101, new ArrayComparer());
private byte[] h160;
public override int HashSize => 32;
public override void Initialize()
{
}
protected override void HashCore(byte[] bytes, int ibStart, int cbSize)
{
h160 = hasher.ComputeHash(bytes, ibStart, cbSize);
map.Add(h160, bytes);
}
protected override byte[] HashFinal()
{
HashValue = (byte[])map.FindKeyIndex(h160).GetBytes().Clone();
return HashValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment