DerT61String.cs 2.4 KB

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