DerBMPString.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 BMPString object.
  9. */
  10. public class DerBmpString
  11. : DerStringBase
  12. {
  13. private readonly string str;
  14. /**
  15. * return a BMP string from the given object.
  16. *
  17. * @param obj the object we want converted.
  18. * @exception ArgumentException if the object cannot be converted.
  19. */
  20. public static DerBmpString GetInstance(
  21. object obj)
  22. {
  23. if (obj == null || obj is DerBmpString)
  24. {
  25. return (DerBmpString)obj;
  26. }
  27. throw new ArgumentException("illegal object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  28. }
  29. /**
  30. * return a BMP 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 DerBmpString GetInstance(
  39. Asn1TaggedObject obj,
  40. bool isExplicit)
  41. {
  42. Asn1Object o = obj.GetObject();
  43. if (isExplicit || o is DerBmpString)
  44. {
  45. return GetInstance(o);
  46. }
  47. return new DerBmpString(Asn1OctetString.GetInstance(o).GetOctets());
  48. }
  49. /**
  50. * basic constructor - byte encoded string.
  51. */
  52. public DerBmpString(
  53. byte[] str)
  54. {
  55. if (str == null)
  56. throw new ArgumentNullException("str");
  57. char[] cs = new char[str.Length / 2];
  58. for (int i = 0; i != cs.Length; i++)
  59. {
  60. cs[i] = (char)((str[2 * i] << 8) | (str[2 * i + 1] & 0xff));
  61. }
  62. this.str = new string(cs);
  63. }
  64. /**
  65. * basic constructor
  66. */
  67. public DerBmpString(
  68. string str)
  69. {
  70. if (str == null)
  71. throw new ArgumentNullException("str");
  72. this.str = str;
  73. }
  74. public override string GetString()
  75. {
  76. return str;
  77. }
  78. protected override bool Asn1Equals(
  79. Asn1Object asn1Object)
  80. {
  81. DerBmpString other = asn1Object as DerBmpString;
  82. if (other == null)
  83. return false;
  84. return this.str.Equals(other.str);
  85. }
  86. internal override void Encode(
  87. DerOutputStream derOut)
  88. {
  89. char[] c = str.ToCharArray();
  90. byte[] b = new byte[c.Length * 2];
  91. for (int i = 0; i != c.Length; i++)
  92. {
  93. b[2 * i] = (byte)(c[i] >> 8);
  94. b[2 * i + 1] = (byte)c[i];
  95. }
  96. derOut.WriteEncoded(Asn1Tags.BmpString, b);
  97. }
  98. }
  99. }
  100. #pragma warning restore
  101. #endif