Asn1OctetString.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using System.IO;
  5. using Org.BouncyCastle.Utilities;
  6. using Org.BouncyCastle.Utilities.Encoders;
  7. namespace Org.BouncyCastle.Asn1
  8. {
  9. public abstract class Asn1OctetString
  10. : Asn1Object, Asn1OctetStringParser
  11. {
  12. internal byte[] str;
  13. /**
  14. * return an Octet string from a tagged object.
  15. *
  16. * @param obj the tagged object holding the object we want.
  17. * @param explicitly true if the object is meant to be explicitly
  18. * tagged false otherwise.
  19. * @exception ArgumentException if the tagged object cannot
  20. * be converted.
  21. */
  22. public static Asn1OctetString GetInstance(
  23. Asn1TaggedObject obj,
  24. bool isExplicit)
  25. {
  26. Asn1Object o = obj.GetObject();
  27. if (isExplicit || o is Asn1OctetString)
  28. {
  29. return GetInstance(o);
  30. }
  31. return BerOctetString.FromSequence(Asn1Sequence.GetInstance(o));
  32. }
  33. /**
  34. * return an Octet string from the given object.
  35. *
  36. * @param obj the object we want converted.
  37. * @exception ArgumentException if the object cannot be converted.
  38. */
  39. public static Asn1OctetString GetInstance(object obj)
  40. {
  41. if (obj == null || obj is Asn1OctetString)
  42. {
  43. return (Asn1OctetString)obj;
  44. }
  45. // TODO: this needs to be deleted in V2
  46. if (obj is Asn1TaggedObject)
  47. return GetInstance(((Asn1TaggedObject)obj).GetObject());
  48. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  49. }
  50. /**
  51. * @param string the octets making up the octet string.
  52. */
  53. internal Asn1OctetString(
  54. byte[] str)
  55. {
  56. if (str == null)
  57. throw new ArgumentNullException("str");
  58. this.str = str;
  59. }
  60. internal Asn1OctetString(
  61. Asn1Encodable obj)
  62. {
  63. try
  64. {
  65. this.str = obj.GetEncoded(Asn1Encodable.Der);
  66. }
  67. catch (IOException e)
  68. {
  69. throw new ArgumentException("Error processing object : " + e.ToString());
  70. }
  71. }
  72. public Stream GetOctetStream()
  73. {
  74. return new MemoryStream(str, false);
  75. }
  76. public Asn1OctetStringParser Parser
  77. {
  78. get { return this; }
  79. }
  80. public virtual byte[] GetOctets()
  81. {
  82. return str;
  83. }
  84. protected override int Asn1GetHashCode()
  85. {
  86. return Arrays.GetHashCode(GetOctets());
  87. }
  88. protected override bool Asn1Equals(
  89. Asn1Object asn1Object)
  90. {
  91. DerOctetString other = asn1Object as DerOctetString;
  92. if (other == null)
  93. return false;
  94. return Arrays.AreEqual(GetOctets(), other.GetOctets());
  95. }
  96. public override string ToString()
  97. {
  98. return "#" + Hex.ToHexString(str);
  99. }
  100. }
  101. }
  102. #endif