Mapping160BitTo64BitHash.cs

Maps a 160-Bit Hash Into a 64-Bit Space

using System.Security.Cryptography;
/// <summary>
/// Pros: ~no Collisions within long bit space ~(9,223,372,036,854,775,807)
/// Cons: Maintains a TinyDictionary(byte[],byte[]), non-deterministic across application or
/// method domains
/// Cannot Transform or reuse.
/// </summary>
public class Mapping160BitTo64BitHash : HashAlgorithm
{
public readonly TinyDictionary<byte[], byte[]> map = new TinyDictionary<byte[], byte[]>(101, new ArrayComparer());
private byte[] h160;
private readonly SHA1Managed hasher = new SHA1Managed();
public override int HashSize => 64;
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)
{
h160 = hasher.ComputeHash(bytes, ibStart, cbSize);
map.Add(h160, bytes);
}
/// <inheritdoc />
/// <summary>
/// Return the unique position within the TinyDictionary as the hash value (index).
/// </summary>
protected override byte[] HashFinal()
{
HashValue = (byte[])map.FindKeyIndex(h160).GetBytes().Clone();
return HashValue;
}
}
using System.Security.Cryptography; /// <summary> /// Pros: ~no Collisions within long bit space ~(9,223,372,036,854,775,807) /// Cons: Maintains a TinyDictionary(byte[],byte[]), non-deterministic across application or /// method domains /// Cannot Transform or reuse. /// </summary> public class Mapping160BitTo64BitHash : HashAlgorithm { public readonly TinyDictionary<byte[], byte[]> map = new TinyDictionary<byte[], byte[]>(101, new ArrayComparer()); private byte[] h160; private readonly SHA1Managed hasher = new SHA1Managed(); public override int HashSize => 64; 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) { h160 = hasher.ComputeHash(bytes, ibStart, cbSize); map.Add(h160, bytes); } /// <inheritdoc /> /// <summary> /// Return the unique position within the TinyDictionary as the hash value (index). /// </summary> protected override byte[] HashFinal() { HashValue = (byte[])map.FindKeyIndex(h160).GetBytes().Clone(); return HashValue; } }
using System.Security.Cryptography;
/// <summary>
///     Pros: ~no Collisions within long bit space ~(9,223,372,036,854,775,807)
///     Cons: Maintains a TinyDictionary(byte[],byte[]), non-deterministic across application or
///     method domains
///     Cannot Transform or reuse.
/// </summary>
public class Mapping160BitTo64BitHash : HashAlgorithm
{
    public readonly  TinyDictionary<byte[], byte[]> map = new TinyDictionary<byte[], byte[]>(101, new ArrayComparer());
    private          byte[]                         h160;
    private readonly SHA1Managed                    hasher = new SHA1Managed();
    public override  int                            HashSize => 64;
    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)
    {
        h160 = hasher.ComputeHash(bytes, ibStart, cbSize);
        map.Add(h160, bytes);
    }
    /// <inheritdoc />
    /// <summary>
    ///     Return the unique position within the TinyDictionary as the hash value (index).
    /// </summary>
    protected override byte[] HashFinal()
    {
        HashValue = (byte[])map.FindKeyIndex(h160).GetBytes().Clone();
        return HashValue;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *