BufferedCipherBase.cs 2.5 KB

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