Asn1Encodable.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System.IO;
  4. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  5. {
  6. public abstract class Asn1Encodable
  7. : IAsn1Convertible
  8. {
  9. public const string Der = "DER";
  10. public const string Ber = "BER";
  11. public byte[] GetEncoded()
  12. {
  13. MemoryStream bOut = new MemoryStream();
  14. Asn1OutputStream aOut = new Asn1OutputStream(bOut);
  15. aOut.WriteObject(this);
  16. return bOut.ToArray();
  17. }
  18. public byte[] GetEncoded(
  19. string encoding)
  20. {
  21. if (encoding.Equals(Der))
  22. {
  23. MemoryStream bOut = new MemoryStream();
  24. DerOutputStream dOut = new DerOutputStream(bOut);
  25. dOut.WriteObject(this);
  26. return bOut.ToArray();
  27. }
  28. return GetEncoded();
  29. }
  30. /**
  31. * Return the DER encoding of the object, null if the DER encoding can not be made.
  32. *
  33. * @return a DER byte array, null otherwise.
  34. */
  35. public byte[] GetDerEncoded()
  36. {
  37. try
  38. {
  39. return GetEncoded(Der);
  40. }
  41. catch (IOException)
  42. {
  43. return null;
  44. }
  45. }
  46. public sealed override int GetHashCode()
  47. {
  48. return ToAsn1Object().CallAsn1GetHashCode();
  49. }
  50. public sealed override bool Equals(
  51. object obj)
  52. {
  53. if (obj == this)
  54. return true;
  55. IAsn1Convertible other = obj as IAsn1Convertible;
  56. if (other == null)
  57. return false;
  58. Asn1Object o1 = ToAsn1Object();
  59. Asn1Object o2 = other.ToAsn1Object();
  60. return o1 == o2 || o1.CallAsn1Equals(o2);
  61. }
  62. public abstract Asn1Object ToAsn1Object();
  63. }
  64. }
  65. #pragma warning restore
  66. #endif