ZeroBytePadding.cs 1.8 KB

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