SessionId.cs

Session Id Class Hash Reduction Mapping

using System;
using System.Numerics;
using System.Security.Cryptography;
public class SessionId
{
    private readonly SHA512Managed                _hash = new SHA512Managed();
    private readonly AcDictionary<byte[], byte[]> map   = new AcDictionary<byte[], byte[]>(1024, new ArrayComparer());
    public byte[] GetUniqueSessionId<T>(T obj, int bitWidth)
    {
        if (bitWidth < 32 || bitWidth > 256)
            throw new ArgumentException($"Bit Width {bitWidth} must be between 32 and 256.");
        var bytes = obj.GetBytes();
        var hash  = _hash.ComputeHash(bytes, 0, bytes.Length);
        map.Add(hash, bytes);
        var ba = (byte[]) map.FindKeyIndex(hash).GetBytes().Clone();
        return ba.SubArray(0, bitWidth >> 3);
    }
    public int GetUniqueSessionIntId<T>(T obj)
    {
        var bytes = obj.GetBytes();
        var hash  = _hash.ComputeHash(bytes, 0, bytes.Length);
        map.Add(hash, bytes);
        return map.FindKeyIndex(hash);
    }
    public BigInteger PermutationWithRepetition(BigInteger n, int r)
    {
        if (r == BigInteger.Zero)
            return BigInteger.Zero;
        if (n == BigInteger.Zero)
            return BigInteger.Zero;
        return BigInteger.Pow(n, r) - 1;
    }
}