DerUniversalString.cs 3.1 KB

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