DerNumericString.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 NumericString object - this is an ascii string of characters {0,1,2,3,4,5,6,7,8,9, }.
  9. */
  10. public class DerNumericString
  11. : DerStringBase
  12. {
  13. private readonly string str;
  14. /**
  15. * return a Numeric string from the passed in object
  16. *
  17. * @exception ArgumentException if the object cannot be converted.
  18. */
  19. public static DerNumericString GetInstance(
  20. object obj)
  21. {
  22. if (obj == null || obj is DerNumericString)
  23. {
  24. return (DerNumericString)obj;
  25. }
  26. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  27. }
  28. /**
  29. * return an Numeric 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 DerNumericString GetInstance(
  38. Asn1TaggedObject obj,
  39. bool isExplicit)
  40. {
  41. Asn1Object o = obj.GetObject();
  42. if (isExplicit || o is DerNumericString)
  43. {
  44. return GetInstance(o);
  45. }
  46. return new DerNumericString(Asn1OctetString.GetInstance(o).GetOctets());
  47. }
  48. /**
  49. * basic constructor - with bytes.
  50. */
  51. public DerNumericString(
  52. byte[] str)
  53. : this(Strings.FromAsciiByteArray(str), false)
  54. {
  55. }
  56. /**
  57. * basic constructor - without validation..
  58. */
  59. public DerNumericString(
  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 a NumericString.
  71. */
  72. public DerNumericString(
  73. string str,
  74. bool validate)
  75. {
  76. if (str == null)
  77. throw new ArgumentNullException("str");
  78. if (validate && !IsNumericString(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.NumericString, GetOctets());
  94. }
  95. protected override bool Asn1Equals(
  96. Asn1Object asn1Object)
  97. {
  98. DerNumericString other = asn1Object as DerNumericString;
  99. if (other == null)
  100. return false;
  101. return this.str.Equals(other.str);
  102. }
  103. /**
  104. * Return true if the string can be represented as a NumericString ('0'..'9', ' ')
  105. *
  106. * @param str string to validate.
  107. * @return true if numeric, fale otherwise.
  108. */
  109. public static bool IsNumericString(
  110. string str)
  111. {
  112. foreach (char ch in str)
  113. {
  114. if (ch > 0x007f || (ch != ' ' && !char.IsDigit(ch)))
  115. return false;
  116. }
  117. return true;
  118. }
  119. }
  120. }
  121. #endif