BufferedIesCipher.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto
  9. {
  10. public class BufferedIesCipher
  11. : BufferedCipherBase
  12. {
  13. private readonly IesEngine engine;
  14. private bool forEncryption;
  15. private MemoryStream buffer = new MemoryStream();
  16. public BufferedIesCipher(
  17. IesEngine engine)
  18. {
  19. if (engine == null)
  20. throw new ArgumentNullException("engine");
  21. this.engine = engine;
  22. }
  23. public override string AlgorithmName
  24. {
  25. // TODO Create IESEngine.AlgorithmName
  26. get { return "IES"; }
  27. }
  28. public override void Init(
  29. bool forEncryption,
  30. ICipherParameters parameters)
  31. {
  32. this.forEncryption = forEncryption;
  33. // TODO
  34. throw BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateNotImplementedException("IES");
  35. }
  36. public override int GetBlockSize()
  37. {
  38. return 0;
  39. }
  40. public override int GetOutputSize(
  41. int inputLen)
  42. {
  43. if (engine == null)
  44. throw new InvalidOperationException("cipher not initialised");
  45. int baseLen = inputLen + (int) buffer.Length;
  46. return forEncryption
  47. ? baseLen + 20
  48. : baseLen - 20;
  49. }
  50. public override int GetUpdateOutputSize(
  51. int inputLen)
  52. {
  53. return 0;
  54. }
  55. public override byte[] ProcessByte(
  56. byte input)
  57. {
  58. buffer.WriteByte(input);
  59. return null;
  60. }
  61. public override byte[] ProcessBytes(
  62. byte[] input,
  63. int inOff,
  64. int length)
  65. {
  66. if (input == null)
  67. throw new ArgumentNullException("input");
  68. if (inOff < 0)
  69. throw new ArgumentException("inOff");
  70. if (length < 0)
  71. throw new ArgumentException("length");
  72. if (inOff + length > input.Length)
  73. throw new ArgumentException("invalid offset/length specified for input array");
  74. buffer.Write(input, inOff, length);
  75. return null;
  76. }
  77. public override byte[] DoFinal()
  78. {
  79. byte[] buf = buffer.ToArray();
  80. Reset();
  81. return engine.ProcessBlock(buf, 0, buf.Length);
  82. }
  83. public override byte[] DoFinal(
  84. byte[] input,
  85. int inOff,
  86. int length)
  87. {
  88. ProcessBytes(input, inOff, length);
  89. return DoFinal();
  90. }
  91. public override void Reset()
  92. {
  93. buffer.SetLength(0);
  94. }
  95. }
  96. }
  97. #pragma warning restore
  98. #endif