Skip to content

Instantly share code, notes, and snippets.

@mjs3339
Created January 12, 2021 14:21
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/2698070579cd0ec14c53930a92d320b8 to your computer and use it in GitHub Desktop.
Save mjs3339/2698070579cd0ec14c53930a92d320b8 to your computer and use it in GitHub Desktop.
Modified Sha3
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment