DerVisibleString.cs 2.7 KB

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