X923Padding.cs 1.9 KB

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