SignatureSubpacket.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System.IO;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Bcpg
  6. {
  7. /// <remarks>Basic type for a PGP Signature sub-packet.</remarks>
  8. public class SignatureSubpacket
  9. {
  10. private readonly SignatureSubpacketTag type;
  11. private readonly bool critical;
  12. private readonly bool isLongLength;
  13. internal byte[] data;
  14. protected internal SignatureSubpacket(
  15. SignatureSubpacketTag type,
  16. bool critical,
  17. bool isLongLength,
  18. byte[] data)
  19. {
  20. this.type = type;
  21. this.critical = critical;
  22. this.isLongLength = isLongLength;
  23. this.data = data;
  24. }
  25. public SignatureSubpacketTag SubpacketType
  26. {
  27. get { return type; }
  28. }
  29. public bool IsCritical()
  30. {
  31. return critical;
  32. }
  33. public bool IsLongLength()
  34. {
  35. return isLongLength;
  36. }
  37. /// <summary>Return the generic data making up the packet.</summary>
  38. public byte[] GetData()
  39. {
  40. return (byte[]) data.Clone();
  41. }
  42. public void Encode(
  43. Stream os)
  44. {
  45. int bodyLen = data.Length + 1;
  46. if (isLongLength)
  47. {
  48. os.WriteByte(0xff);
  49. os.WriteByte((byte)(bodyLen >> 24));
  50. os.WriteByte((byte)(bodyLen >> 16));
  51. os.WriteByte((byte)(bodyLen >> 8));
  52. os.WriteByte((byte)bodyLen);
  53. }
  54. else
  55. {
  56. if (bodyLen < 192)
  57. {
  58. os.WriteByte((byte)bodyLen);
  59. }
  60. else if (bodyLen <= 8383)
  61. {
  62. bodyLen -= 192;
  63. os.WriteByte((byte)(((bodyLen >> 8) & 0xff) + 192));
  64. os.WriteByte((byte)bodyLen);
  65. }
  66. else
  67. {
  68. os.WriteByte(0xff);
  69. os.WriteByte((byte)(bodyLen >> 24));
  70. os.WriteByte((byte)(bodyLen >> 16));
  71. os.WriteByte((byte)(bodyLen >> 8));
  72. os.WriteByte((byte)bodyLen);
  73. }
  74. }
  75. if (critical)
  76. {
  77. os.WriteByte((byte)(0x80 | (int) type));
  78. }
  79. else
  80. {
  81. os.WriteByte((byte) type);
  82. }
  83. os.Write(data, 0, data.Length);
  84. }
  85. public override int GetHashCode()
  86. {
  87. return (critical ? 1 : 0) + 7 * (int)type + 49 * Arrays.GetHashCode(data);
  88. }
  89. public override bool Equals(object obj)
  90. {
  91. if (obj == this)
  92. return true;
  93. SignatureSubpacket other = obj as SignatureSubpacket;
  94. if (null == other)
  95. return false;
  96. return this.type == other.type
  97. && this.critical == other.critical
  98. && Arrays.AreEqual(this.data, other.data);
  99. }
  100. }
  101. }
  102. #pragma warning restore
  103. #endif