BufferedIesCipher.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto
  7. {
  8. public class BufferedIesCipher
  9. : BufferedCipherBase
  10. {
  11. private readonly IesEngine engine;
  12. private bool forEncryption;
  13. private MemoryStream buffer = new MemoryStream();
  14. public BufferedIesCipher(
  15. IesEngine engine)
  16. {
  17. if (engine == null)
  18. throw new ArgumentNullException("engine");
  19. this.engine = engine;
  20. }
  21. public override string AlgorithmName
  22. {
  23. // TODO Create IESEngine.AlgorithmName
  24. get { return "IES"; }
  25. }
  26. public override void Init(
  27. bool forEncryption,
  28. ICipherParameters parameters)
  29. {
  30. this.forEncryption = forEncryption;
  31. // TODO
  32. throw new NotImplementedException("IES");
  33. }
  34. public override int GetBlockSize()
  35. {
  36. return 0;
  37. }
  38. public override int GetOutputSize(
  39. int inputLen)
  40. {
  41. if (engine == null)
  42. throw new InvalidOperationException("cipher not initialised");
  43. int baseLen = inputLen + Convert.ToInt32(buffer.Length);
  44. return forEncryption
  45. ? baseLen + 20
  46. : baseLen - 20;
  47. }
  48. public override int GetUpdateOutputSize(
  49. int inputLen)
  50. {
  51. return 0;
  52. }
  53. public override byte[] ProcessByte(byte input)
  54. {
  55. buffer.WriteByte(input);
  56. return null;
  57. }
  58. public override int ProcessByte(byte input, byte[] output, int outOff)
  59. {
  60. buffer.WriteByte(input);
  61. return 0;
  62. }
  63. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  64. public override int ProcessByte(byte input, Span<byte> output)
  65. {
  66. buffer.WriteByte(input);
  67. return 0;
  68. }
  69. #endif
  70. public override byte[] ProcessBytes(
  71. byte[] input,
  72. int inOff,
  73. int length)
  74. {
  75. if (input == null)
  76. throw new ArgumentNullException("input");
  77. if (inOff < 0)
  78. throw new ArgumentException("inOff");
  79. if (length < 0)
  80. throw new ArgumentException("length");
  81. if (inOff + length > input.Length)
  82. throw new ArgumentException("invalid offset/length specified for input array");
  83. buffer.Write(input, inOff, length);
  84. return null;
  85. }
  86. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  87. public override int ProcessBytes(ReadOnlySpan<byte> input, Span<byte> output)
  88. {
  89. buffer.Write(input);
  90. return 0;
  91. }
  92. #endif
  93. public override byte[] DoFinal()
  94. {
  95. byte[] buf = buffer.ToArray();
  96. Reset();
  97. return engine.ProcessBlock(buf, 0, buf.Length);
  98. }
  99. public override byte[] DoFinal(
  100. byte[] input,
  101. int inOff,
  102. int length)
  103. {
  104. ProcessBytes(input, inOff, length);
  105. return DoFinal();
  106. }
  107. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  108. public override int DoFinal(Span<byte> output)
  109. {
  110. byte[] buf = buffer.ToArray();
  111. Reset();
  112. byte[] block = engine.ProcessBlock(buf, 0, buf.Length);
  113. block.CopyTo(output);
  114. return block.Length;
  115. }
  116. #endif
  117. public override void Reset()
  118. {
  119. buffer.SetLength(0);
  120. }
  121. }
  122. }
  123. #pragma warning restore
  124. #endif