init commit
This commit is contained in:
@@ -0,0 +1,214 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.IO;
|
||||
|
||||
public class CipherStream : Stream
|
||||
{
|
||||
internal Stream stream;
|
||||
|
||||
internal IBufferedCipher inCipher;
|
||||
|
||||
internal IBufferedCipher outCipher;
|
||||
|
||||
private byte[] mInBuf;
|
||||
|
||||
private int mInPos;
|
||||
|
||||
private bool inStreamEnded;
|
||||
|
||||
public IBufferedCipher ReadCipher => inCipher;
|
||||
|
||||
public IBufferedCipher WriteCipher => outCipher;
|
||||
|
||||
public override bool CanRead
|
||||
{
|
||||
get
|
||||
{
|
||||
if (stream.CanRead)
|
||||
{
|
||||
return inCipher != null;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanWrite
|
||||
{
|
||||
get
|
||||
{
|
||||
if (stream.CanWrite)
|
||||
{
|
||||
return outCipher != null;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanSeek => false;
|
||||
|
||||
public sealed override long Length
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed override long Position
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
set
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
public CipherStream(Stream stream, IBufferedCipher readCipher, IBufferedCipher writeCipher)
|
||||
{
|
||||
this.stream = stream;
|
||||
if (readCipher != null)
|
||||
{
|
||||
inCipher = readCipher;
|
||||
mInBuf = null;
|
||||
}
|
||||
if (writeCipher != null)
|
||||
{
|
||||
outCipher = writeCipher;
|
||||
}
|
||||
}
|
||||
|
||||
public override int ReadByte()
|
||||
{
|
||||
if (inCipher == null)
|
||||
{
|
||||
return stream.ReadByte();
|
||||
}
|
||||
if ((mInBuf == null || mInPos >= mInBuf.Length) && !FillInBuf())
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return mInBuf[mInPos++];
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (inCipher == null)
|
||||
{
|
||||
return stream.Read(buffer, offset, count);
|
||||
}
|
||||
int i;
|
||||
int num;
|
||||
for (i = 0; i < count; i += num)
|
||||
{
|
||||
if ((mInBuf == null || mInPos >= mInBuf.Length) && !FillInBuf())
|
||||
{
|
||||
break;
|
||||
}
|
||||
num = System.Math.Min(count - i, mInBuf.Length - mInPos);
|
||||
Array.Copy(mInBuf, mInPos, buffer, offset + i, num);
|
||||
mInPos += num;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
private bool FillInBuf()
|
||||
{
|
||||
if (inStreamEnded)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
mInPos = 0;
|
||||
do
|
||||
{
|
||||
mInBuf = ReadAndProcessBlock();
|
||||
}
|
||||
while (!inStreamEnded && mInBuf == null);
|
||||
return mInBuf != null;
|
||||
}
|
||||
|
||||
private byte[] ReadAndProcessBlock()
|
||||
{
|
||||
int blockSize = inCipher.GetBlockSize();
|
||||
int num = ((blockSize == 0) ? 256 : blockSize);
|
||||
byte[] array = new byte[num];
|
||||
int num2 = 0;
|
||||
do
|
||||
{
|
||||
int num3 = stream.Read(array, num2, array.Length - num2);
|
||||
if (num3 < 1)
|
||||
{
|
||||
inStreamEnded = true;
|
||||
break;
|
||||
}
|
||||
num2 += num3;
|
||||
}
|
||||
while (num2 < array.Length);
|
||||
byte[] array2 = (inStreamEnded ? inCipher.DoFinal(array, 0, num2) : inCipher.ProcessBytes(array));
|
||||
if (array2 != null && array2.Length == 0)
|
||||
{
|
||||
array2 = null;
|
||||
}
|
||||
return array2;
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int num = offset + count;
|
||||
if (outCipher == null)
|
||||
{
|
||||
stream.Write(buffer, offset, count);
|
||||
return;
|
||||
}
|
||||
byte[] array = outCipher.ProcessBytes(buffer, offset, count);
|
||||
if (array != null)
|
||||
{
|
||||
stream.Write(array, 0, array.Length);
|
||||
}
|
||||
}
|
||||
|
||||
public override void WriteByte(byte b)
|
||||
{
|
||||
if (outCipher == null)
|
||||
{
|
||||
stream.WriteByte(b);
|
||||
return;
|
||||
}
|
||||
byte[] array = outCipher.ProcessByte(b);
|
||||
if (array != null)
|
||||
{
|
||||
stream.Write(array, 0, array.Length);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
if (outCipher != null)
|
||||
{
|
||||
byte[] array = outCipher.DoFinal();
|
||||
stream.Write(array, 0, array.Length);
|
||||
stream.Flush();
|
||||
}
|
||||
Platform.Dispose(stream);
|
||||
base.Close();
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
stream.Flush();
|
||||
}
|
||||
|
||||
public sealed override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public sealed override void SetLength(long length)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.IO;
|
||||
|
||||
public class DigestSink : BaseOutputStream
|
||||
{
|
||||
private readonly IDigest mDigest;
|
||||
|
||||
public virtual IDigest Digest => mDigest;
|
||||
|
||||
public DigestSink(IDigest digest)
|
||||
{
|
||||
mDigest = digest;
|
||||
}
|
||||
|
||||
public override void WriteByte(byte b)
|
||||
{
|
||||
mDigest.Update(b);
|
||||
}
|
||||
|
||||
public override void Write(byte[] buf, int off, int len)
|
||||
{
|
||||
if (len > 0)
|
||||
{
|
||||
mDigest.BlockUpdate(buf, off, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.IO;
|
||||
|
||||
public class DigestStream : Stream
|
||||
{
|
||||
protected readonly Stream stream;
|
||||
|
||||
protected readonly IDigest inDigest;
|
||||
|
||||
protected readonly IDigest outDigest;
|
||||
|
||||
public override bool CanRead => stream.CanRead;
|
||||
|
||||
public override bool CanWrite => stream.CanWrite;
|
||||
|
||||
public override bool CanSeek => stream.CanSeek;
|
||||
|
||||
public override long Length => stream.Length;
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get
|
||||
{
|
||||
return stream.Position;
|
||||
}
|
||||
set
|
||||
{
|
||||
stream.Position = value;
|
||||
}
|
||||
}
|
||||
|
||||
public DigestStream(Stream stream, IDigest readDigest, IDigest writeDigest)
|
||||
{
|
||||
this.stream = stream;
|
||||
inDigest = readDigest;
|
||||
outDigest = writeDigest;
|
||||
}
|
||||
|
||||
public virtual IDigest ReadDigest()
|
||||
{
|
||||
return inDigest;
|
||||
}
|
||||
|
||||
public virtual IDigest WriteDigest()
|
||||
{
|
||||
return outDigest;
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int num = stream.Read(buffer, offset, count);
|
||||
if (inDigest != null && num > 0)
|
||||
{
|
||||
inDigest.BlockUpdate(buffer, offset, num);
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
public override int ReadByte()
|
||||
{
|
||||
int num = stream.ReadByte();
|
||||
if (inDigest != null && num >= 0)
|
||||
{
|
||||
inDigest.Update((byte)num);
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (outDigest != null && count > 0)
|
||||
{
|
||||
outDigest.BlockUpdate(buffer, offset, count);
|
||||
}
|
||||
stream.Write(buffer, offset, count);
|
||||
}
|
||||
|
||||
public override void WriteByte(byte b)
|
||||
{
|
||||
if (outDigest != null)
|
||||
{
|
||||
outDigest.Update(b);
|
||||
}
|
||||
stream.WriteByte(b);
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
Platform.Dispose(stream);
|
||||
base.Close();
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
stream.Flush();
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
return stream.Seek(offset, origin);
|
||||
}
|
||||
|
||||
public override void SetLength(long length)
|
||||
{
|
||||
stream.SetLength(length);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.IO;
|
||||
|
||||
public class MacSink : BaseOutputStream
|
||||
{
|
||||
private readonly IMac mMac;
|
||||
|
||||
public virtual IMac Mac => mMac;
|
||||
|
||||
public MacSink(IMac mac)
|
||||
{
|
||||
mMac = mac;
|
||||
}
|
||||
|
||||
public override void WriteByte(byte b)
|
||||
{
|
||||
mMac.Update(b);
|
||||
}
|
||||
|
||||
public override void Write(byte[] buf, int off, int len)
|
||||
{
|
||||
if (len > 0)
|
||||
{
|
||||
mMac.BlockUpdate(buf, off, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.IO;
|
||||
|
||||
public class MacStream : Stream
|
||||
{
|
||||
protected readonly Stream stream;
|
||||
|
||||
protected readonly IMac inMac;
|
||||
|
||||
protected readonly IMac outMac;
|
||||
|
||||
public override bool CanRead => stream.CanRead;
|
||||
|
||||
public override bool CanWrite => stream.CanWrite;
|
||||
|
||||
public override bool CanSeek => stream.CanSeek;
|
||||
|
||||
public override long Length => stream.Length;
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get
|
||||
{
|
||||
return stream.Position;
|
||||
}
|
||||
set
|
||||
{
|
||||
stream.Position = value;
|
||||
}
|
||||
}
|
||||
|
||||
public MacStream(Stream stream, IMac readMac, IMac writeMac)
|
||||
{
|
||||
this.stream = stream;
|
||||
inMac = readMac;
|
||||
outMac = writeMac;
|
||||
}
|
||||
|
||||
public virtual IMac ReadMac()
|
||||
{
|
||||
return inMac;
|
||||
}
|
||||
|
||||
public virtual IMac WriteMac()
|
||||
{
|
||||
return outMac;
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int num = stream.Read(buffer, offset, count);
|
||||
if (inMac != null && num > 0)
|
||||
{
|
||||
inMac.BlockUpdate(buffer, offset, num);
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
public override int ReadByte()
|
||||
{
|
||||
int num = stream.ReadByte();
|
||||
if (inMac != null && num >= 0)
|
||||
{
|
||||
inMac.Update((byte)num);
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (outMac != null && count > 0)
|
||||
{
|
||||
outMac.BlockUpdate(buffer, offset, count);
|
||||
}
|
||||
stream.Write(buffer, offset, count);
|
||||
}
|
||||
|
||||
public override void WriteByte(byte b)
|
||||
{
|
||||
if (outMac != null)
|
||||
{
|
||||
outMac.Update(b);
|
||||
}
|
||||
stream.WriteByte(b);
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
Platform.Dispose(stream);
|
||||
base.Close();
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
stream.Flush();
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
return stream.Seek(offset, origin);
|
||||
}
|
||||
|
||||
public override void SetLength(long length)
|
||||
{
|
||||
stream.SetLength(length);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.IO;
|
||||
|
||||
public class SignerSink : BaseOutputStream
|
||||
{
|
||||
private readonly ISigner mSigner;
|
||||
|
||||
public virtual ISigner Signer => mSigner;
|
||||
|
||||
public SignerSink(ISigner signer)
|
||||
{
|
||||
mSigner = signer;
|
||||
}
|
||||
|
||||
public override void WriteByte(byte b)
|
||||
{
|
||||
mSigner.Update(b);
|
||||
}
|
||||
|
||||
public override void Write(byte[] buf, int off, int len)
|
||||
{
|
||||
if (len > 0)
|
||||
{
|
||||
mSigner.BlockUpdate(buf, off, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.IO;
|
||||
|
||||
public class SignerStream : Stream
|
||||
{
|
||||
protected readonly Stream stream;
|
||||
|
||||
protected readonly ISigner inSigner;
|
||||
|
||||
protected readonly ISigner outSigner;
|
||||
|
||||
public override bool CanRead => stream.CanRead;
|
||||
|
||||
public override bool CanWrite => stream.CanWrite;
|
||||
|
||||
public override bool CanSeek => stream.CanSeek;
|
||||
|
||||
public override long Length => stream.Length;
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get
|
||||
{
|
||||
return stream.Position;
|
||||
}
|
||||
set
|
||||
{
|
||||
stream.Position = value;
|
||||
}
|
||||
}
|
||||
|
||||
public SignerStream(Stream stream, ISigner readSigner, ISigner writeSigner)
|
||||
{
|
||||
this.stream = stream;
|
||||
inSigner = readSigner;
|
||||
outSigner = writeSigner;
|
||||
}
|
||||
|
||||
public virtual ISigner ReadSigner()
|
||||
{
|
||||
return inSigner;
|
||||
}
|
||||
|
||||
public virtual ISigner WriteSigner()
|
||||
{
|
||||
return outSigner;
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int num = stream.Read(buffer, offset, count);
|
||||
if (inSigner != null && num > 0)
|
||||
{
|
||||
inSigner.BlockUpdate(buffer, offset, num);
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
public override int ReadByte()
|
||||
{
|
||||
int num = stream.ReadByte();
|
||||
if (inSigner != null && num >= 0)
|
||||
{
|
||||
inSigner.Update((byte)num);
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (outSigner != null && count > 0)
|
||||
{
|
||||
outSigner.BlockUpdate(buffer, offset, count);
|
||||
}
|
||||
stream.Write(buffer, offset, count);
|
||||
}
|
||||
|
||||
public override void WriteByte(byte b)
|
||||
{
|
||||
if (outSigner != null)
|
||||
{
|
||||
outSigner.Update(b);
|
||||
}
|
||||
stream.WriteByte(b);
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
Platform.Dispose(stream);
|
||||
base.Close();
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
stream.Flush();
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
return stream.Seek(offset, origin);
|
||||
}
|
||||
|
||||
public override void SetLength(long length)
|
||||
{
|
||||
stream.SetLength(length);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user