DerIA5String.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 IA5String object - this is an ascii string.
  9. */
  10. public class DerIA5String
  11. : DerStringBase
  12. {
  13. private readonly string str;
  14. /**
  15. * return a IA5 string from the passed in object
  16. *
  17. * @exception ArgumentException if the object cannot be converted.
  18. */
  19. public static DerIA5String GetInstance(
  20. object obj)
  21. {
  22. if (obj == null || obj is DerIA5String)
  23. {
  24. return (DerIA5String)obj;
  25. }
  26. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  27. }
  28. /**
  29. * return an IA5 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 DerIA5String GetInstance(
  38. Asn1TaggedObject obj,
  39. bool isExplicit)
  40. {
  41. Asn1Object o = obj.GetObject();
  42. if (isExplicit || o is DerIA5String)
  43. {
  44. return GetInstance(o);
  45. }
  46. return new DerIA5String(((Asn1OctetString)o).GetOctets());
  47. }
  48. /**
  49. * basic constructor - with bytes.
  50. */
  51. public DerIA5String(
  52. byte[] str)
  53. : this(Strings.FromAsciiByteArray(str), false)
  54. {
  55. }
  56. /**
  57. * basic constructor - without validation.
  58. */
  59. public DerIA5String(
  60. string str)
  61. : this(str, false)
  62. {
  63. }
  64. /**
  65. * Constructor with optional validation.
  66. *
  67. * @param string the base string to wrap.
  68. * @param validate whether or not to check the string.
  69. * @throws ArgumentException if validate is true and the string
  70. * contains characters that should not be in an IA5String.
  71. */
  72. public DerIA5String(
  73. string str,
  74. bool validate)
  75. {
  76. if (str == null)
  77. throw new ArgumentNullException("str");
  78. if (validate && !IsIA5String(str))
  79. throw new ArgumentException("string contains illegal characters", "str");
  80. this.str = str;
  81. }
  82. public override string GetString()
  83. {
  84. return str;
  85. }
  86. public byte[] GetOctets()
  87. {
  88. return Strings.ToAsciiByteArray(str);
  89. }
  90. internal override void Encode(
  91. DerOutputStream derOut)
  92. {
  93. derOut.WriteEncoded(Asn1Tags.IA5String, GetOctets());
  94. }
  95. protected override int Asn1GetHashCode()
  96. {
  97. return this.str.GetHashCode();
  98. }
  99. protected override bool Asn1Equals(
  100. Asn1Object asn1Object)
  101. {
  102. DerIA5String other = asn1Object as DerIA5String;
  103. if (other == null)
  104. return false;
  105. return this.str.Equals(other.str);
  106. }
  107. /**
  108. * return true if the passed in String can be represented without
  109. * loss as an IA5String, false otherwise.
  110. *
  111. * @return true if in printable set, false otherwise.
  112. */
  113. public static bool IsIA5String(
  114. string str)
  115. {
  116. foreach (char ch in str)
  117. {
  118. if (ch > 0x007f)
  119. {
  120. return false;
  121. }
  122. }
  123. return true;
  124. }
  125. }
  126. }
  127. #endif