SignerStream.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.IO
  7. {
  8. public class SignerStream
  9. : Stream
  10. {
  11. protected readonly Stream stream;
  12. protected readonly ISigner inSigner;
  13. protected readonly ISigner outSigner;
  14. public SignerStream(
  15. Stream stream,
  16. ISigner readSigner,
  17. ISigner writeSigner)
  18. {
  19. this.stream = stream;
  20. this.inSigner = readSigner;
  21. this.outSigner = writeSigner;
  22. }
  23. public virtual ISigner ReadSigner()
  24. {
  25. return inSigner;
  26. }
  27. public virtual ISigner WriteSigner()
  28. {
  29. return outSigner;
  30. }
  31. public override int Read(
  32. byte[] buffer,
  33. int offset,
  34. int count)
  35. {
  36. int n = stream.Read(buffer, offset, count);
  37. if (inSigner != null)
  38. {
  39. if (n > 0)
  40. {
  41. inSigner.BlockUpdate(buffer, offset, n);
  42. }
  43. }
  44. return n;
  45. }
  46. public override int ReadByte()
  47. {
  48. int b = stream.ReadByte();
  49. if (inSigner != null)
  50. {
  51. if (b >= 0)
  52. {
  53. inSigner.Update((byte)b);
  54. }
  55. }
  56. return b;
  57. }
  58. public override void Write(
  59. byte[] buffer,
  60. int offset,
  61. int count)
  62. {
  63. if (outSigner != null)
  64. {
  65. if (count > 0)
  66. {
  67. outSigner.BlockUpdate(buffer, offset, count);
  68. }
  69. }
  70. stream.Write(buffer, offset, count);
  71. }
  72. public override void WriteByte(
  73. byte b)
  74. {
  75. if (outSigner != null)
  76. {
  77. outSigner.Update(b);
  78. }
  79. stream.WriteByte(b);
  80. }
  81. public override bool CanRead
  82. {
  83. get { return stream.CanRead; }
  84. }
  85. public override bool CanWrite
  86. {
  87. get { return stream.CanWrite; }
  88. }
  89. public override bool CanSeek
  90. {
  91. get { return stream.CanSeek; }
  92. }
  93. public override long Length
  94. {
  95. get { return stream.Length; }
  96. }
  97. public override long Position
  98. {
  99. get { return stream.Position; }
  100. set { stream.Position = value; }
  101. }
  102. #if PORTABLE || NETFX_CORE
  103. protected override void Dispose(bool disposing)
  104. {
  105. if (disposing)
  106. {
  107. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(stream);
  108. }
  109. base.Dispose(disposing);
  110. }
  111. #else
  112. public override void Close()
  113. {
  114. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(stream);
  115. base.Close();
  116. }
  117. #endif
  118. public override void Flush()
  119. {
  120. stream.Flush();
  121. }
  122. public override long Seek(
  123. long offset,
  124. SeekOrigin origin)
  125. {
  126. return stream.Seek(offset, origin);
  127. }
  128. public override void SetLength(
  129. long length)
  130. {
  131. stream.SetLength(length);
  132. }
  133. }
  134. }
  135. #pragma warning restore
  136. #endif