DerUTF8String.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 UTF8String object.
  9. */
  10. public class DerUtf8String
  11. : DerStringBase
  12. {
  13. private readonly string str;
  14. /**
  15. * return an UTF8 string from the passed in object.
  16. *
  17. * @exception ArgumentException if the object cannot be converted.
  18. */
  19. public static DerUtf8String GetInstance(
  20. object obj)
  21. {
  22. if (obj == null || obj is DerUtf8String)
  23. {
  24. return (DerUtf8String)obj;
  25. }
  26. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  27. }
  28. /**
  29. * return an UTF8 string from a tagged object.
  30. *
  31. * @param obj the tagged object holding the object we want
  32. * @param explicitly true if the object is meant to be explicitly
  33. * tagged false otherwise.
  34. * @exception ArgumentException if the tagged object cannot
  35. * be converted.
  36. */
  37. public static DerUtf8String GetInstance(
  38. Asn1TaggedObject obj,
  39. bool isExplicit)
  40. {
  41. Asn1Object o = obj.GetObject();
  42. if (isExplicit || o is DerUtf8String)
  43. {
  44. return GetInstance(o);
  45. }
  46. return new DerUtf8String(Asn1OctetString.GetInstance(o).GetOctets());
  47. }
  48. /**
  49. * basic constructor - byte encoded string.
  50. */
  51. public DerUtf8String(
  52. byte[] str)
  53. : this(Encoding.UTF8.GetString(str, 0, str.Length))
  54. {
  55. }
  56. /**
  57. * basic constructor
  58. */
  59. public DerUtf8String(
  60. string str)
  61. {
  62. if (str == null)
  63. throw new ArgumentNullException("str");
  64. this.str = str;
  65. }
  66. public override string GetString()
  67. {
  68. return str;
  69. }
  70. protected override bool Asn1Equals(
  71. Asn1Object asn1Object)
  72. {
  73. DerUtf8String other = asn1Object as DerUtf8String;
  74. if (other == null)
  75. return false;
  76. return this.str.Equals(other.str);
  77. }
  78. internal override void Encode(
  79. DerOutputStream derOut)
  80. {
  81. derOut.WriteEncoded(Asn1Tags.Utf8String, Encoding.UTF8.GetBytes(str));
  82. }
  83. }
  84. }
  85. #endif