BufferedStreamCipher.cs 2.3 KB

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