BufferedCipherBase.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. namespace BestHTTP.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. public virtual byte[] ProcessBytes(
  30. byte[] input)
  31. {
  32. return ProcessBytes(input, 0, input.Length);
  33. }
  34. public abstract byte[] ProcessBytes(byte[] input, int inOff, int length);
  35. public virtual int ProcessBytes(
  36. byte[] input,
  37. byte[] output,
  38. int outOff)
  39. {
  40. return ProcessBytes(input, 0, input.Length, output, outOff);
  41. }
  42. public virtual int ProcessBytes(
  43. byte[] input,
  44. int inOff,
  45. int length,
  46. byte[] output,
  47. int outOff)
  48. {
  49. byte[] outBytes = ProcessBytes(input, inOff, length);
  50. if (outBytes == null)
  51. return 0;
  52. if (outOff + outBytes.Length > output.Length)
  53. throw new DataLengthException("output buffer too short");
  54. outBytes.CopyTo(output, outOff);
  55. return outBytes.Length;
  56. }
  57. public abstract byte[] DoFinal();
  58. public virtual byte[] DoFinal(
  59. byte[] input)
  60. {
  61. return DoFinal(input, 0, input.Length);
  62. }
  63. public abstract byte[] DoFinal(
  64. byte[] input,
  65. int inOff,
  66. int length);
  67. public virtual int DoFinal(
  68. byte[] output,
  69. int outOff)
  70. {
  71. byte[] outBytes = DoFinal();
  72. if (outOff + outBytes.Length > output.Length)
  73. throw new DataLengthException("output buffer too short");
  74. outBytes.CopyTo(output, outOff);
  75. return outBytes.Length;
  76. }
  77. public virtual int DoFinal(
  78. byte[] input,
  79. byte[] output,
  80. int outOff)
  81. {
  82. return DoFinal(input, 0, input.Length, output, outOff);
  83. }
  84. public virtual int DoFinal(
  85. byte[] input,
  86. int inOff,
  87. int length,
  88. byte[] output,
  89. int outOff)
  90. {
  91. int len = ProcessBytes(input, inOff, length, output, outOff);
  92. len += DoFinal(output, outOff + len);
  93. return len;
  94. }
  95. public abstract void Reset();
  96. }
  97. }
  98. #pragma warning restore
  99. #endif