DerVideotexString.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Utilities;
  4. namespace Org.BouncyCastle.Asn1
  5. {
  6. public class DerVideotexString
  7. : DerStringBase
  8. {
  9. private readonly byte[] mString;
  10. /**
  11. * return a Videotex String from the passed in object
  12. *
  13. * @param obj a DERVideotexString or an object that can be converted into one.
  14. * @exception IllegalArgumentException if the object cannot be converted.
  15. * @return a DERVideotexString instance, or null.
  16. */
  17. public static DerVideotexString GetInstance(object obj)
  18. {
  19. if (obj == null || obj is DerVideotexString)
  20. {
  21. return (DerVideotexString)obj;
  22. }
  23. if (obj is byte[])
  24. {
  25. try
  26. {
  27. return (DerVideotexString)FromByteArray((byte[])obj);
  28. }
  29. catch (Exception e)
  30. {
  31. throw new ArgumentException("encoding error in GetInstance: " + e.ToString(), "obj");
  32. }
  33. }
  34. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  35. }
  36. /**
  37. * return a Videotex String from a tagged object.
  38. *
  39. * @param obj the tagged object holding the object we want
  40. * @param explicit true if the object is meant to be explicitly
  41. * tagged false otherwise.
  42. * @exception IllegalArgumentException if the tagged object cannot
  43. * be converted.
  44. * @return a DERVideotexString instance, or null.
  45. */
  46. public static DerVideotexString GetInstance(Asn1TaggedObject obj, bool isExplicit)
  47. {
  48. Asn1Object o = obj.GetObject();
  49. if (isExplicit || o is DerVideotexString)
  50. {
  51. return GetInstance(o);
  52. }
  53. return new DerVideotexString(((Asn1OctetString)o).GetOctets());
  54. }
  55. /**
  56. * basic constructor - with bytes.
  57. * @param string the byte encoding of the characters making up the string.
  58. */
  59. public DerVideotexString(byte[] encoding)
  60. {
  61. this.mString = Arrays.Clone(encoding);
  62. }
  63. public override string GetString()
  64. {
  65. return Strings.FromByteArray(mString);
  66. }
  67. public byte[] GetOctets()
  68. {
  69. return Arrays.Clone(mString);
  70. }
  71. internal override void Encode(DerOutputStream derOut)
  72. {
  73. derOut.WriteEncoded(Asn1Tags.VideotexString, mString);
  74. }
  75. protected override int Asn1GetHashCode()
  76. {
  77. return Arrays.GetHashCode(mString);
  78. }
  79. protected override bool Asn1Equals(
  80. Asn1Object asn1Object)
  81. {
  82. DerVideotexString other = asn1Object as DerVideotexString;
  83. if (other == null)
  84. return false;
  85. return Arrays.AreEqual(mString, other.mString);
  86. }
  87. }
  88. }
  89. #endif