ISO7816d4Padding.cs 1.6 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. /**
  9. * A padder that adds the padding according to the scheme referenced in
  10. * ISO 7814-4 - scheme 2 from ISO 9797-1. The first byte is 0x80, rest is 0x00
  11. */
  12. public class ISO7816d4Padding
  13. : IBlockCipherPadding
  14. {
  15. /**
  16. * Initialise the padder.
  17. *
  18. * @param random - a SecureRandom if available.
  19. */
  20. public void Init(
  21. SecureRandom random)
  22. {
  23. // nothing to do.
  24. }
  25. /**
  26. * Return the name of the algorithm the padder implements.
  27. *
  28. * @return the name of the algorithm the padder implements.
  29. */
  30. public string PaddingName
  31. {
  32. get { return "ISO7816-4"; }
  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. int added = (input.Length - inOff);
  43. input[inOff]= (byte) 0x80;
  44. inOff ++;
  45. while (inOff < input.Length)
  46. {
  47. input[inOff] = (byte) 0;
  48. inOff++;
  49. }
  50. return added;
  51. }
  52. /**
  53. * return the number of pad bytes present in the block.
  54. */
  55. public int PadCount(
  56. byte[] input)
  57. {
  58. int count = input.Length - 1;
  59. while (count > 0 && input[count] == 0)
  60. {
  61. count--;
  62. }
  63. if (input[count] != (byte)0x80)
  64. {
  65. throw new InvalidCipherTextException("pad block corrupted");
  66. }
  67. return input.Length - count;
  68. }
  69. }
  70. }
  71. #pragma warning restore
  72. #endif