DerT61String.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  6. {
  7. /**
  8. * Der T61String (also the teletex string) - 8-bit characters
  9. */
  10. public class DerT61String
  11. : DerStringBase
  12. {
  13. private readonly string str;
  14. /**
  15. * return a T61 string from the passed in object.
  16. *
  17. * @exception ArgumentException if the object cannot be converted.
  18. */
  19. public static DerT61String GetInstance(
  20. object obj)
  21. {
  22. if (obj == null || obj is DerT61String)
  23. {
  24. return (DerT61String)obj;
  25. }
  26. throw new ArgumentException("illegal object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  27. }
  28. /**
  29. * return an T61 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 DerT61String GetInstance(
  38. Asn1TaggedObject obj,
  39. bool isExplicit)
  40. {
  41. Asn1Object o = obj.GetObject();
  42. if (isExplicit || o is DerT61String)
  43. {
  44. return GetInstance(o);
  45. }
  46. return new DerT61String(Asn1OctetString.GetInstance(o).GetOctets());
  47. }
  48. /**
  49. * basic constructor - with bytes.
  50. */
  51. public DerT61String(
  52. byte[] str)
  53. : this(Strings.FromByteArray(str))
  54. {
  55. }
  56. /**
  57. * basic constructor - with string.
  58. */
  59. public DerT61String(
  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. internal override int EncodedLength(bool withID)
  71. {
  72. return Asn1OutputStream.GetLengthOfEncodingDL(withID, str.Length);
  73. }
  74. internal override void Encode(Asn1OutputStream asn1Out, bool withID)
  75. {
  76. asn1Out.WriteEncodingDL(withID, Asn1Tags.T61String, GetOctets());
  77. }
  78. public byte[] GetOctets()
  79. {
  80. return Strings.ToByteArray(str);
  81. }
  82. protected override bool Asn1Equals(
  83. Asn1Object asn1Object)
  84. {
  85. DerT61String other = asn1Object as DerT61String;
  86. if (other == null)
  87. return false;
  88. return this.str.Equals(other.str);
  89. }
  90. }
  91. }
  92. #pragma warning restore
  93. #endif