DerUniversalString.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 UniversalString object.
  9. */
  10. public class DerUniversalString
  11. : DerStringBase
  12. {
  13. private static readonly char[] table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  14. private readonly byte[] str;
  15. /**
  16. * return a Universal string from the passed in object.
  17. *
  18. * @exception ArgumentException if the object cannot be converted.
  19. */
  20. public static DerUniversalString GetInstance(
  21. object obj)
  22. {
  23. if (obj == null || obj is DerUniversalString)
  24. {
  25. return (DerUniversalString)obj;
  26. }
  27. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  28. }
  29. /**
  30. * return a Universal 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 DerUniversalString GetInstance(
  39. Asn1TaggedObject obj,
  40. bool isExplicit)
  41. {
  42. Asn1Object o = obj.GetObject();
  43. if (isExplicit || o is DerUniversalString)
  44. {
  45. return GetInstance(o);
  46. }
  47. return new DerUniversalString(Asn1OctetString.GetInstance(o).GetOctets());
  48. }
  49. /**
  50. * basic constructor - byte encoded string.
  51. */
  52. public DerUniversalString(
  53. byte[] str)
  54. {
  55. if (str == null)
  56. throw new ArgumentNullException("str");
  57. this.str = str;
  58. }
  59. public override string GetString()
  60. {
  61. StringBuilder buffer = new StringBuilder("#");
  62. byte[] enc = GetDerEncoded();
  63. for (int i = 0; i != enc.Length; i++)
  64. {
  65. uint ubyte = enc[i];
  66. buffer.Append(table[(ubyte >> 4) & 0xf]);
  67. buffer.Append(table[enc[i] & 0xf]);
  68. }
  69. return buffer.ToString();
  70. }
  71. public byte[] GetOctets()
  72. {
  73. return (byte[]) str.Clone();
  74. }
  75. internal override void Encode(
  76. DerOutputStream derOut)
  77. {
  78. derOut.WriteEncoded(Asn1Tags.UniversalString, this.str);
  79. }
  80. protected override bool Asn1Equals(
  81. Asn1Object asn1Object)
  82. {
  83. DerUniversalString other = asn1Object as DerUniversalString;
  84. if (other == null)
  85. return false;
  86. // return this.GetString().Equals(other.GetString());
  87. return Arrays.AreEqual(this.str, other.str);
  88. }
  89. }
  90. }
  91. #endif