X923Padding.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. /**
  9. * A padder that adds X9.23 padding to a block - if a SecureRandom is
  10. * passed in random padding is assumed, otherwise padding with zeros is used.
  11. */
  12. public class X923Padding
  13. : IBlockCipherPadding
  14. {
  15. private SecureRandom random;
  16. /**
  17. * Initialise the padder.
  18. *
  19. * @param random a SecureRandom if one is available.
  20. */
  21. public void Init(
  22. SecureRandom random)
  23. {
  24. this.random = random;
  25. }
  26. /**
  27. * Return the name of the algorithm the cipher implements.
  28. *
  29. * @return the name of the algorithm the cipher implements.
  30. */
  31. public string PaddingName
  32. {
  33. get { return "X9.23"; }
  34. }
  35. /**
  36. * add the pad bytes to the passed in block, returning the
  37. * number of bytes added.
  38. */
  39. public int AddPadding(
  40. byte[] input,
  41. int inOff)
  42. {
  43. byte code = (byte)(input.Length - inOff);
  44. while (inOff < input.Length - 1)
  45. {
  46. if (random == null)
  47. {
  48. input[inOff] = 0;
  49. }
  50. else
  51. {
  52. input[inOff] = (byte)random.NextInt();
  53. }
  54. inOff++;
  55. }
  56. input[inOff] = code;
  57. return code;
  58. }
  59. /**
  60. * return the number of pad bytes present in the block.
  61. */
  62. public int PadCount(
  63. byte[] input)
  64. {
  65. int count = input[input.Length - 1] & 0xff;
  66. if (count > input.Length)
  67. {
  68. throw new InvalidCipherTextException("pad block corrupted");
  69. }
  70. return count;
  71. }
  72. }
  73. }
  74. #pragma warning restore
  75. #endif