BufferedStreamCipher.cs 2.2 KB

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