TbcPadding.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 Trailing-Bit-Compliment padding to a block.
  9. /// <p>
  10. /// This padding pads the block out compliment of the last bit
  11. /// of the plain text.
  12. /// </p>
  13. /// </summary>
  14. public class TbcPadding
  15. : IBlockCipherPadding
  16. {
  17. /// <summary> Return the name of the algorithm the cipher implements.</summary>
  18. /// <returns> the name of the algorithm the cipher implements.
  19. /// </returns>
  20. public string PaddingName
  21. {
  22. get { return "TBC"; }
  23. }
  24. /// <summary> Initialise the padder.</summary>
  25. /// <param name="random">- a SecureRandom if available.
  26. /// </param>
  27. public virtual void Init(SecureRandom random)
  28. {
  29. // nothing to do.
  30. }
  31. /// <summary> add the pad bytes to the passed in block, returning the
  32. /// number of bytes added.
  33. /// <p>
  34. /// Note: this assumes that the last block of plain text is always
  35. /// passed to it inside in. i.e. if inOff is zero, indicating the
  36. /// entire block is to be overwritten with padding the value of in
  37. /// should be the same as the last block of plain text.
  38. /// </p>
  39. /// </summary>
  40. public virtual int AddPadding(byte[] input, int inOff)
  41. {
  42. int count = input.Length - inOff;
  43. byte code;
  44. if (inOff > 0)
  45. {
  46. code = (byte)((input[inOff - 1] & 0x01) == 0?0xff:0x00);
  47. }
  48. else
  49. {
  50. code = (byte)((input[input.Length - 1] & 0x01) == 0?0xff:0x00);
  51. }
  52. while (inOff < input.Length)
  53. {
  54. input[inOff] = code;
  55. inOff++;
  56. }
  57. return count;
  58. }
  59. /// <summary> return the number of pad bytes present in the block.</summary>
  60. public virtual int PadCount(byte[] input)
  61. {
  62. byte code = input[input.Length - 1];
  63. int index = input.Length - 1;
  64. while (index > 0 && input[index - 1] == code)
  65. {
  66. index--;
  67. }
  68. return input.Length - index;
  69. }
  70. }
  71. }
  72. #pragma warning restore
  73. #endif