MacStream.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 MacStream
  9. : Stream
  10. {
  11. protected readonly Stream stream;
  12. protected readonly IMac inMac;
  13. protected readonly IMac outMac;
  14. public MacStream(
  15. Stream stream,
  16. IMac readMac,
  17. IMac writeMac)
  18. {
  19. this.stream = stream;
  20. this.inMac = readMac;
  21. this.outMac = writeMac;
  22. }
  23. public virtual IMac ReadMac()
  24. {
  25. return inMac;
  26. }
  27. public virtual IMac WriteMac()
  28. {
  29. return outMac;
  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 (inMac != null)
  38. {
  39. if (n > 0)
  40. {
  41. inMac.BlockUpdate(buffer, offset, n);
  42. }
  43. }
  44. return n;
  45. }
  46. public override int ReadByte()
  47. {
  48. int b = stream.ReadByte();
  49. if (inMac != null)
  50. {
  51. if (b >= 0)
  52. {
  53. inMac.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 (outMac != null)
  64. {
  65. if (count > 0)
  66. {
  67. outMac.BlockUpdate(buffer, offset, count);
  68. }
  69. }
  70. stream.Write(buffer, offset, count);
  71. }
  72. public override void WriteByte(byte b)
  73. {
  74. if (outMac != null)
  75. {
  76. outMac.Update(b);
  77. }
  78. stream.WriteByte(b);
  79. }
  80. public override bool CanRead
  81. {
  82. get { return stream.CanRead; }
  83. }
  84. public override bool CanWrite
  85. {
  86. get { return stream.CanWrite; }
  87. }
  88. public override bool CanSeek
  89. {
  90. get { return stream.CanSeek; }
  91. }
  92. public override long Length
  93. {
  94. get { return stream.Length; }
  95. }
  96. public override long Position
  97. {
  98. get { return stream.Position; }
  99. set { stream.Position = value; }
  100. }
  101. #if PORTABLE || NETFX_CORE
  102. protected override void Dispose(bool disposing)
  103. {
  104. if (disposing)
  105. {
  106. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(stream);
  107. }
  108. base.Dispose(disposing);
  109. }
  110. #else
  111. public override void Close()
  112. {
  113. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(stream);
  114. base.Close();
  115. }
  116. #endif
  117. public override void Flush()
  118. {
  119. stream.Flush();
  120. }
  121. public override long Seek(
  122. long offset,
  123. SeekOrigin origin)
  124. {
  125. return stream.Seek(offset,origin);
  126. }
  127. public override void SetLength(
  128. long length)
  129. {
  130. stream.SetLength(length);
  131. }
  132. }
  133. }
  134. #pragma warning restore
  135. #endif