SHA3ModInt.cs

Modified Sha3 used with JitterCacheRng.cs

using System;
[Serializable]
public class SHA3ModInt : HashAlgorithmEx
{
    private readonly KeccakSpongeManaged _sponge;
    public SHA3ModInt(int size, int rounds = 24, ulong[] seed = null)
    {
        if (rounds > 24)
            throw new Exception($"Maximum rounds allowed is {24}");
        var MaxBR     = (64 >> 3) * 25;
        var sizeBytes = size >> 3;
        var rateBytes = MaxBR - (sizeBytes << 1);
        var MaxSize   = ((MaxBR - 8)       >> 1) << 3;
        if (rateBytes < 8)
            throw new Exception($"Maximum size allowed is {MaxSize} Bits with {64} bit Width specified. Specified Size is {size} Bits.");
        var outputLength = size >> 3;
        _sponge        = new KeccakSpongeManaged(rateBytes, 200 - rateBytes, KeccakSpongeManaged.KeccakDelimiter, outputLength, seed);
        _sponge.Rounds = rounds;
        HashSizeValue  = size;
    }
    public override void Initialize()
    {
        _sponge.Initialize();
    }
    protected override void HashCore(byte[] array, int ibStart, int cbSize)
    {
        Initialize();
        _sponge.Absorb(array, ibStart, cbSize);
    }
    protected override byte[] HashFinal()
    {
        return _sponge.Squeeze();
    }
}

Leave a Reply

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