Asn1Encodable.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 virtual void EncodeTo(Stream output)
  12. {
  13. ToAsn1Object().EncodeTo(output);
  14. }
  15. public virtual void EncodeTo(Stream output, string encoding)
  16. {
  17. ToAsn1Object().EncodeTo(output, encoding);
  18. }
  19. public byte[] GetEncoded()
  20. {
  21. MemoryStream bOut = new MemoryStream();
  22. EncodeTo(bOut);
  23. return bOut.ToArray();
  24. }
  25. public byte[] GetEncoded(string encoding)
  26. {
  27. MemoryStream bOut = new MemoryStream();
  28. EncodeTo(bOut, encoding);
  29. return bOut.ToArray();
  30. }
  31. /**
  32. * Return the DER encoding of the object, null if the DER encoding can not be made.
  33. *
  34. * @return a DER byte array, null otherwise.
  35. */
  36. public byte[] GetDerEncoded()
  37. {
  38. try
  39. {
  40. return GetEncoded(Der);
  41. }
  42. catch (IOException)
  43. {
  44. return null;
  45. }
  46. }
  47. public sealed override int GetHashCode()
  48. {
  49. return ToAsn1Object().CallAsn1GetHashCode();
  50. }
  51. public sealed override bool Equals(
  52. object obj)
  53. {
  54. if (obj == this)
  55. return true;
  56. IAsn1Convertible other = obj as IAsn1Convertible;
  57. if (other == null)
  58. return false;
  59. Asn1Object o1 = ToAsn1Object();
  60. Asn1Object o2 = other.ToAsn1Object();
  61. return o1 == o2 || (null != o2 && o1.CallAsn1Equals(o2));
  62. }
  63. public abstract Asn1Object ToAsn1Object();
  64. }
  65. }
  66. #pragma warning restore
  67. #endif