123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- using System.IO;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
- namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.IO
- {
- public sealed class SignerStream
- : Stream
- {
- private readonly Stream stream;
- private readonly ISigner inSigner;
- private readonly ISigner outSigner;
- public SignerStream(Stream stream, ISigner readSigner, ISigner writeSigner)
- {
- this.stream = stream;
- this.inSigner = readSigner;
- this.outSigner = writeSigner;
- }
- public ISigner ReadSigner => inSigner;
- public ISigner WriteSigner => outSigner;
- public override bool CanRead
- {
- get { return stream.CanRead; }
- }
- public sealed override bool CanSeek
- {
- get { return false; }
- }
- public override bool CanWrite
- {
- get { return stream.CanWrite; }
- }
- #if NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER || (UNITY_2021_2_OR_NEWER && (NET_STANDARD_2_0 || NET_STANDARD_2_1))
- public override void CopyTo(Stream destination, int bufferSize)
- {
- if (inSigner == null)
- {
- stream.CopyTo(destination, bufferSize);
- }
- else
- {
- base.CopyTo(destination, bufferSize);
- }
- }
- #endif
- public override void Flush()
- {
- stream.Flush();
- }
- public sealed override long Length
- {
- get { throw new NotSupportedException(); }
- }
- public sealed override long Position
- {
- get { throw new NotSupportedException(); }
- set { throw new NotSupportedException(); }
- }
- public override int Read(byte[] buffer, int offset, int count)
- {
- int n = stream.Read(buffer, offset, count);
- if (inSigner != null && n > 0)
- {
- inSigner.BlockUpdate(buffer, offset, n);
- }
- return n;
- }
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
- public override int Read(Span<byte> buffer)
- {
- int n = stream.Read(buffer);
- if (inSigner != null && n > 0)
- {
- inSigner.BlockUpdate(buffer[..n]);
- }
- return n;
- }
- #endif
- public override int ReadByte()
- {
- int b = stream.ReadByte();
- if (inSigner != null && b >= 0)
- {
- inSigner.Update((byte)b);
- }
- return b;
- }
- public sealed override long Seek(long offset, SeekOrigin origin)
- {
- throw new NotSupportedException();
- }
- public sealed override void SetLength(long length)
- {
- throw new NotSupportedException();
- }
- public override void Write(byte[] buffer, int offset, int count)
- {
- stream.Write(buffer, offset, count);
- if (outSigner != null && count > 0)
- {
- outSigner.BlockUpdate(buffer, offset, count);
- }
- }
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
- public override void Write(ReadOnlySpan<byte> buffer)
- {
- stream.Write(buffer);
- if (outSigner != null && !buffer.IsEmpty)
- {
- outSigner.BlockUpdate(buffer);
- }
- }
- #endif
- public override void WriteByte(byte value)
- {
- stream.WriteByte(value);
- if (outSigner != null)
- {
- outSigner.Update(value);
- }
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- stream.Dispose();
- }
- base.Dispose(disposing);
- }
- }
- }
- #pragma warning restore
- #endif
|