123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- using System.IO;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
- namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.IO
- {
- public class DigestStream
- : Stream
- {
- protected readonly Stream stream;
- protected readonly IDigest inDigest;
- protected readonly IDigest outDigest;
- public DigestStream(
- Stream stream,
- IDigest readDigest,
- IDigest writeDigest)
- {
- this.stream = stream;
- this.inDigest = readDigest;
- this.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 n = stream.Read(buffer, offset, count);
- if (inDigest != null)
- {
- if (n > 0)
- {
- inDigest.BlockUpdate(buffer, offset, n);
- }
- }
- return n;
- }
- public override int ReadByte()
- {
- int b = stream.ReadByte();
- if (inDigest != null)
- {
- if (b >= 0)
- {
- inDigest.Update((byte)b);
- }
- }
- return b;
- }
- public override void Write(
- byte[] buffer,
- int offset,
- int count)
- {
- if (outDigest != null)
- {
- if (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 bool CanRead
- {
- get { return stream.CanRead; }
- }
- public override bool CanWrite
- {
- get { return stream.CanWrite; }
- }
- public override bool CanSeek
- {
- get { return stream.CanSeek; }
- }
- public override long Length
- {
- get { return stream.Length; }
- }
- public override long Position
- {
- get { return stream.Position; }
- set { stream.Position = value; }
- }
- #if PORTABLE || NETFX_CORE
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(stream);
- }
- base.Dispose(disposing);
- }
- #else
- public override void Close()
- {
- BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(stream);
- base.Close();
- }
- #endif
- 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);
- }
- }
- }
- #pragma warning restore
- #endif
|