BufferedStreamCipher.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto
  6. {
  7. public class BufferedStreamCipher
  8. : BufferedCipherBase
  9. {
  10. private readonly IStreamCipher m_cipher;
  11. public BufferedStreamCipher(IStreamCipher cipher)
  12. {
  13. if (cipher == null)
  14. throw new ArgumentNullException("cipher");
  15. this.m_cipher = cipher;
  16. }
  17. public override string AlgorithmName
  18. {
  19. get { return m_cipher.AlgorithmName; }
  20. }
  21. public override void Init(bool forEncryption, ICipherParameters parameters)
  22. {
  23. if (parameters is ParametersWithRandom withRandom)
  24. {
  25. parameters = withRandom.Parameters;
  26. }
  27. m_cipher.Init(forEncryption, parameters);
  28. }
  29. public override int GetBlockSize()
  30. {
  31. return 0;
  32. }
  33. public override int GetOutputSize(int inputLen)
  34. {
  35. return inputLen;
  36. }
  37. public override int GetUpdateOutputSize(int inputLen)
  38. {
  39. return inputLen;
  40. }
  41. public override byte[] ProcessByte(byte input)
  42. {
  43. return new byte[]{ m_cipher.ReturnByte(input) };
  44. }
  45. public override int ProcessByte(byte input, byte[] output, int outOff)
  46. {
  47. if (outOff >= output.Length)
  48. throw new DataLengthException("output buffer too short");
  49. output[outOff] = m_cipher.ReturnByte(input);
  50. return 1;
  51. }
  52. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  53. public override int ProcessByte(byte input, Span<byte> output)
  54. {
  55. output[0] = m_cipher.ReturnByte(input);
  56. return 1;
  57. }
  58. #endif
  59. public override byte[] ProcessBytes(byte[] input, int inOff, int length)
  60. {
  61. if (length < 1)
  62. return null;
  63. byte[] output = new byte[length];
  64. m_cipher.ProcessBytes(input, inOff, length, output, 0);
  65. return output;
  66. }
  67. public override int ProcessBytes(byte[] input, int inOff, int length, byte[] output, int outOff)
  68. {
  69. if (length < 1)
  70. return 0;
  71. m_cipher.ProcessBytes(input, inOff, length, output, outOff);
  72. return length;
  73. }
  74. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  75. public override int ProcessBytes(ReadOnlySpan<byte> input, Span<byte> output)
  76. {
  77. m_cipher.ProcessBytes(input, output);
  78. return input.Length;
  79. }
  80. #endif
  81. public override byte[] DoFinal()
  82. {
  83. m_cipher.Reset();
  84. return EmptyBuffer;
  85. }
  86. public override byte[] DoFinal(byte[] input, int inOff, int length)
  87. {
  88. if (length < 1)
  89. return EmptyBuffer;
  90. byte[] output = new byte[length];
  91. m_cipher.ProcessBytes(input, inOff, length, output, 0);
  92. m_cipher.Reset();
  93. return output;
  94. }
  95. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  96. public override int DoFinal(Span<byte> output)
  97. {
  98. m_cipher.Reset();
  99. return 0;
  100. }
  101. public override int DoFinal(ReadOnlySpan<byte> input, Span<byte> output)
  102. {
  103. m_cipher.ProcessBytes(input, output);
  104. m_cipher.Reset();
  105. return input.Length;
  106. }
  107. #endif
  108. public override void Reset()
  109. {
  110. m_cipher.Reset();
  111. }
  112. }
  113. }
  114. #pragma warning restore
  115. #endif