DerUTF8String.cs 2.7 KB

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