Asn1Encodable.cs 1.5 KB

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