DerNumericString.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Text;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  7. {
  8. /**
  9. * Der NumericString object - this is an ascii string of characters {0,1,2,3,4,5,6,7,8,9, }.
  10. */
  11. public class DerNumericString
  12. : DerStringBase
  13. {
  14. private readonly string str;
  15. /**
  16. * return a Numeric string from the passed in object
  17. *
  18. * @exception ArgumentException if the object cannot be converted.
  19. */
  20. public static DerNumericString GetInstance(
  21. object obj)
  22. {
  23. if (obj == null || obj is DerNumericString)
  24. {
  25. return (DerNumericString)obj;
  26. }
  27. throw new ArgumentException("illegal object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  28. }
  29. /**
  30. * return an Numeric string from a tagged object.
  31. *
  32. * @param obj the tagged object holding the object we want
  33. * @param explicitly true if the object is meant to be explicitly
  34. * tagged false otherwise.
  35. * @exception ArgumentException if the tagged object cannot
  36. * be converted.
  37. */
  38. public static DerNumericString GetInstance(
  39. Asn1TaggedObject obj,
  40. bool isExplicit)
  41. {
  42. Asn1Object o = obj.GetObject();
  43. if (isExplicit || o is DerNumericString)
  44. {
  45. return GetInstance(o);
  46. }
  47. return new DerNumericString(Asn1OctetString.GetInstance(o).GetOctets());
  48. }
  49. /**
  50. * basic constructor - with bytes.
  51. */
  52. public DerNumericString(
  53. byte[] str)
  54. : this(Strings.FromAsciiByteArray(str), false)
  55. {
  56. }
  57. /**
  58. * basic constructor - without validation..
  59. */
  60. public DerNumericString(
  61. string str)
  62. : this(str, false)
  63. {
  64. }
  65. /**
  66. * Constructor with optional validation.
  67. *
  68. * @param string the base string to wrap.
  69. * @param validate whether or not to check the string.
  70. * @throws ArgumentException if validate is true and the string
  71. * contains characters that should not be in a NumericString.
  72. */
  73. public DerNumericString(
  74. string str,
  75. bool validate)
  76. {
  77. if (str == null)
  78. throw new ArgumentNullException("str");
  79. if (validate && !IsNumericString(str))
  80. throw new ArgumentException("string contains illegal characters", "str");
  81. this.str = str;
  82. }
  83. public override string GetString()
  84. {
  85. return str;
  86. }
  87. public byte[] GetOctets()
  88. {
  89. return Strings.ToAsciiByteArray(str);
  90. }
  91. internal override int EncodedLength(bool withID)
  92. {
  93. return Asn1OutputStream.GetLengthOfEncodingDL(withID, str.Length);
  94. }
  95. internal override void Encode(Asn1OutputStream asn1Out, bool withID)
  96. {
  97. asn1Out.WriteEncodingDL(withID, Asn1Tags.NumericString, GetOctets());
  98. }
  99. protected override bool Asn1Equals(
  100. Asn1Object asn1Object)
  101. {
  102. DerNumericString other = asn1Object as DerNumericString;
  103. if (other == null)
  104. return false;
  105. return this.str.Equals(other.str);
  106. }
  107. /**
  108. * Return true if the string can be represented as a NumericString ('0'..'9', ' ')
  109. *
  110. * @param str string to validate.
  111. * @return true if numeric, fale otherwise.
  112. */
  113. public static bool IsNumericString(
  114. string str)
  115. {
  116. foreach (char ch in str)
  117. {
  118. if (ch > 0x007f || (ch != ' ' && !char.IsDigit(ch)))
  119. return false;
  120. }
  121. return true;
  122. }
  123. }
  124. }
  125. #pragma warning restore
  126. #endif