ZeroBytePadding.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto;
  4. using Org.BouncyCastle.Security;
  5. namespace Org.BouncyCastle.Crypto.Paddings
  6. {
  7. /// <summary> A padder that adds Null byte padding to a block.</summary>
  8. public class ZeroBytePadding : IBlockCipherPadding
  9. {
  10. /// <summary> Return the name of the algorithm the cipher implements.
  11. ///
  12. /// </summary>
  13. /// <returns> the name of the algorithm the cipher implements.
  14. /// </returns>
  15. public string PaddingName
  16. {
  17. get { return "ZeroBytePadding"; }
  18. }
  19. /// <summary> Initialise the padder.
  20. ///
  21. /// </summary>
  22. /// <param name="random">- a SecureRandom if available.
  23. /// </param>
  24. public void Init(SecureRandom random)
  25. {
  26. // nothing to do.
  27. }
  28. /// <summary> add the pad bytes to the passed in block, returning the
  29. /// number of bytes added.
  30. /// </summary>
  31. public int AddPadding(
  32. byte[] input,
  33. int inOff)
  34. {
  35. int added = (input.Length - inOff);
  36. while (inOff < input.Length)
  37. {
  38. input[inOff] = (byte) 0;
  39. inOff++;
  40. }
  41. return added;
  42. }
  43. /// <summary> return the number of pad bytes present in the block.</summary>
  44. public int PadCount(
  45. byte[] input)
  46. {
  47. int count = input.Length;
  48. while (count > 0)
  49. {
  50. if (input[count - 1] != 0)
  51. {
  52. break;
  53. }
  54. count--;
  55. }
  56. return input.Length - count;
  57. }
  58. }
  59. }
  60. #endif