BufferedCipherBase.cs 3.2 KB

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