BufferedIesCipher.cs 2.2 KB

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