DerVisibleString.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Text;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  7. {
  8. /**
  9. * Der VisibleString object.
  10. */
  11. public class DerVisibleString
  12. : DerStringBase
  13. {
  14. private readonly string str;
  15. /**
  16. * return a Visible string from the passed in object.
  17. *
  18. * @exception ArgumentException if the object cannot be converted.
  19. */
  20. public static DerVisibleString GetInstance(
  21. object obj)
  22. {
  23. if (obj == null || obj is DerVisibleString)
  24. {
  25. return (DerVisibleString)obj;
  26. }
  27. if (obj is Asn1OctetString)
  28. {
  29. return new DerVisibleString(((Asn1OctetString)obj).GetOctets());
  30. }
  31. if (obj is Asn1TaggedObject)
  32. {
  33. return GetInstance(((Asn1TaggedObject)obj).GetObject());
  34. }
  35. throw new ArgumentException("illegal object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  36. }
  37. /**
  38. * return a Visible string from a tagged object.
  39. *
  40. * @param obj the tagged object holding the object we want
  41. * @param explicitly true if the object is meant to be explicitly
  42. * tagged false otherwise.
  43. * @exception ArgumentException if the tagged object cannot
  44. * be converted.
  45. */
  46. public static DerVisibleString GetInstance(
  47. Asn1TaggedObject obj,
  48. bool explicitly)
  49. {
  50. return GetInstance(obj.GetObject());
  51. }
  52. /**
  53. * basic constructor - byte encoded string.
  54. */
  55. public DerVisibleString(
  56. byte[] str)
  57. : this(Strings.FromAsciiByteArray(str))
  58. {
  59. }
  60. /**
  61. * basic constructor
  62. */
  63. public DerVisibleString(
  64. string str)
  65. {
  66. if (str == null)
  67. throw new ArgumentNullException("str");
  68. this.str = str;
  69. }
  70. public override string GetString()
  71. {
  72. return str;
  73. }
  74. public byte[] GetOctets()
  75. {
  76. return Strings.ToAsciiByteArray(str);
  77. }
  78. internal override int EncodedLength(bool withID)
  79. {
  80. return Asn1OutputStream.GetLengthOfEncodingDL(withID, str.Length);
  81. }
  82. internal override void Encode(Asn1OutputStream asn1Out, bool withID)
  83. {
  84. asn1Out.WriteEncodingDL(withID, Asn1Tags.VisibleString, GetOctets());
  85. }
  86. protected override bool Asn1Equals(
  87. Asn1Object asn1Object)
  88. {
  89. DerVisibleString other = asn1Object as DerVisibleString;
  90. if (other == null)
  91. return false;
  92. return this.str.Equals(other.str);
  93. }
  94. protected override int Asn1GetHashCode()
  95. {
  96. return this.str.GetHashCode();
  97. }
  98. }
  99. }
  100. #pragma warning restore
  101. #endif