Created
January 12, 2021 14:09
-
-
Save mjs3339/f412ff82136ff2b36b9b18f24ed99939 to your computer and use it in GitHub Desktop.
Mapping 256Bit Hash to 32Bit Indexed Hash
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; | |
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