DerGeneralString.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. public class DerGeneralString
  8. : DerStringBase
  9. {
  10. private readonly string str;
  11. public static DerGeneralString GetInstance(
  12. object obj)
  13. {
  14. if (obj == null || obj is DerGeneralString)
  15. {
  16. return (DerGeneralString) obj;
  17. }
  18. throw new ArgumentException("illegal object in GetInstance: "
  19. + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  20. }
  21. public static DerGeneralString GetInstance(
  22. Asn1TaggedObject obj,
  23. bool isExplicit)
  24. {
  25. Asn1Object o = obj.GetObject();
  26. if (isExplicit || o is DerGeneralString)
  27. {
  28. return GetInstance(o);
  29. }
  30. return new DerGeneralString(((Asn1OctetString)o).GetOctets());
  31. }
  32. public DerGeneralString(
  33. byte[] str)
  34. : this(Strings.FromAsciiByteArray(str))
  35. {
  36. }
  37. public DerGeneralString(
  38. string str)
  39. {
  40. if (str == null)
  41. throw new ArgumentNullException("str");
  42. this.str = str;
  43. }
  44. public override string GetString()
  45. {
  46. return str;
  47. }
  48. public byte[] GetOctets()
  49. {
  50. return Strings.ToAsciiByteArray(str);
  51. }
  52. internal override void Encode(
  53. DerOutputStream derOut)
  54. {
  55. derOut.WriteEncoded(Asn1Tags.GeneralString, GetOctets());
  56. }
  57. protected override bool Asn1Equals(
  58. Asn1Object asn1Object)
  59. {
  60. DerGeneralString other = asn1Object as DerGeneralString;
  61. if (other == null)
  62. return false;
  63. return this.str.Equals(other.str);
  64. }
  65. }
  66. }
  67. #endif