<PackageReference Include="BouncyCastle.Cryptography" Version="2.3.0" />

BufferedAeadCipher

using Org.BouncyCastle.Crypto.Modes; using Org.BouncyCastle.Crypto.Parameters; using System; namespace Org.BouncyCastle.Crypto { public class BufferedAeadCipher : BufferedCipherBase { private readonly IAeadCipher cipher; public override string AlgorithmName => cipher.AlgorithmName; public BufferedAeadCipher(IAeadCipher cipher) { if (cipher == null) throw new ArgumentNullException("cipher"); this.cipher = cipher; } public override void Init(bool forEncryption, ICipherParameters parameters) { ParametersWithRandom parametersWithRandom = parameters as ParametersWithRandom; if (parametersWithRandom != null) parameters = parametersWithRandom.Parameters; cipher.Init(forEncryption, parameters); } public override int GetBlockSize() { return 0; } public override int GetUpdateOutputSize(int length) { return cipher.GetUpdateOutputSize(length); } public override int GetOutputSize(int length) { return cipher.GetOutputSize(length); } public override int ProcessByte(byte input, byte[] output, int outOff) { return cipher.ProcessByte(input, output, outOff); } public override byte[] ProcessByte(byte input) { int updateOutputSize = GetUpdateOutputSize(1); byte[] array = (updateOutputSize > 0) ? new byte[updateOutputSize] : null; int num = ProcessByte(input, array, 0); if (updateOutputSize > 0 && num < updateOutputSize) { byte[] array2 = new byte[num]; Array.Copy(array, 0, array2, 0, num); array = array2; } return array; } public override int ProcessByte(byte input, Span<byte> output) { return cipher.ProcessByte(input, output); } public override byte[] ProcessBytes(byte[] input, int inOff, int length) { if (input == null) throw new ArgumentNullException("input"); if (length < 1) return null; int updateOutputSize = GetUpdateOutputSize(length); byte[] array = (updateOutputSize > 0) ? new byte[updateOutputSize] : null; int num = ProcessBytes(input, inOff, length, array, 0); if (updateOutputSize > 0 && num < updateOutputSize) { byte[] array2 = new byte[num]; Array.Copy(array, 0, array2, 0, num); array = array2; } return array; } public override int ProcessBytes(byte[] input, int inOff, int length, byte[] output, int outOff) { return cipher.ProcessBytes(input, inOff, length, output, outOff); } public override int ProcessBytes(ReadOnlySpan<byte> input, Span<byte> output) { return cipher.ProcessBytes(input, output); } public override byte[] DoFinal() { byte[] array = new byte[GetOutputSize(0)]; int num = DoFinal(array, 0); if (num < array.Length) { byte[] array2 = new byte[num]; Array.Copy(array, 0, array2, 0, num); array = array2; } return array; } public override byte[] DoFinal(byte[] input, int inOff, int inLen) { if (input == null) throw new ArgumentNullException("input"); byte[] array = new byte[GetOutputSize(inLen)]; int num = (inLen > 0) ? ProcessBytes(input, inOff, inLen, array, 0) : 0; num += DoFinal(array, num); if (num < array.Length) { byte[] array2 = new byte[num]; Array.Copy(array, 0, array2, 0, num); array = array2; } return array; } public override int DoFinal(byte[] output, int outOff) { return cipher.DoFinal(output, outOff); } public override int DoFinal(Span<byte> output) { return cipher.DoFinal(output); } public override int DoFinal(ReadOnlySpan<byte> input, Span<byte> output) { int num = cipher.ProcessBytes(input, output); int num2 = num; IAeadCipher aeadCipher = cipher; int num3 = num; return num2 + aeadCipher.DoFinal(output.Slice(num3, output.Length - num3)); } public override void Reset() { cipher.Reset(); } } }