using System.Security.Cryptography;

namespace KyberDotnet.Shake
{
	/// <summary>
	/// https://github.com/MrMatthewLayton/CORE/blob/master/Core/Security/Cryptography/Shake256Managed.cs
	/// </summary>
	public class Shake256 : HashAlgorithm
	{
		private readonly KeccakSponge sponge;

		public Shake256(int outputLength) => sponge = new KeccakSponge(136, 64, KeccakSponge.ShakeDelimiter, outputLength);


		public override void Initialize() => sponge.Initialize();

		/// <summary>
		/// Routes data written to the object into the hash algorithm for computing the hash.
		/// </summary>
		/// <param name="array">The input to compute the hash code for.</param>
		/// <param name="ibStart">The offset into the byte array from which to begin using data.</param>
		/// <param name="cbSize">The number of bytes in the byte array to use as data.</param>
		protected override void HashCore(byte[] array, int ibStart, int cbSize)
		{
			Initialize();
			sponge.Absorb(array, ibStart, cbSize);
		}

		/// <summary>
		/// Finalizes the hash computation after the last data is processed by the cryptographic stream object.
		/// </summary>
		/// <returns>The computed hash code.</returns>
		protected override byte[] HashFinal() => sponge.Squeeze();
	}
}
