BufferedAsymmetricBlockCipher
using System;
namespace Org.BouncyCastle.Crypto
{
public class BufferedAsymmetricBlockCipher : BufferedCipherBase
{
private readonly IAsymmetricBlockCipher cipher;
private byte[] buffer;
private int bufOff;
public override string AlgorithmName => cipher.AlgorithmName;
public BufferedAsymmetricBlockCipher(IAsymmetricBlockCipher cipher)
{
this.cipher = cipher;
}
internal int GetBufferPosition()
{
return bufOff;
}
public override int GetBlockSize()
{
return cipher.GetInputBlockSize();
}
public override int GetOutputSize(int length)
{
return cipher.GetOutputBlockSize();
}
public override int GetUpdateOutputSize(int length)
{
return 0;
}
public override void Init(bool forEncryption, ICipherParameters parameters)
{
Reset();
cipher.Init(forEncryption, parameters);
buffer = new byte[cipher.GetInputBlockSize() + (forEncryption ? 1 : 0)];
bufOff = 0;
}
public override byte[] ProcessByte(byte input)
{
Check.DataLength(bufOff >= buffer.Length, "attempt to process message too long for cipher");
buffer[bufOff++] = input;
return null;
}
public override int ProcessByte(byte input, byte[] output, int outOff)
{
Check.DataLength(bufOff >= buffer.Length, "attempt to process message too long for cipher");
buffer[bufOff++] = input;
return 0;
}
public override byte[] ProcessBytes(byte[] input, int inOff, int length)
{
if (length < 1)
return null;
if (input == null)
throw new ArgumentNullException("input");
Check.DataLength(length > buffer.Length - bufOff, "attempt to process message too long for cipher");
Array.Copy(input, inOff, buffer, bufOff, length);
bufOff += length;
return null;
}
public override byte[] DoFinal()
{
byte[] result = (bufOff > 0) ? cipher.ProcessBlock(buffer, 0, bufOff) : BufferedCipherBase.EmptyBuffer;
Reset();
return result;
}
public override byte[] DoFinal(byte[] input, int inOff, int length)
{
ProcessBytes(input, inOff, length);
return DoFinal();
}
public override void Reset()
{
if (buffer != null) {
Array.Clear(buffer, 0, buffer.Length);
bufOff = 0;
}
}
}
}