DerEnumerated.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Math;
  4. using Org.BouncyCastle.Utilities;
  5. namespace Org.BouncyCastle.Asn1
  6. {
  7. public class DerEnumerated
  8. : Asn1Object
  9. {
  10. private readonly byte[] bytes;
  11. /**
  12. * return an integer from the passed in object
  13. *
  14. * @exception ArgumentException if the object cannot be converted.
  15. */
  16. public static DerEnumerated GetInstance(
  17. object obj)
  18. {
  19. if (obj == null || obj is DerEnumerated)
  20. {
  21. return (DerEnumerated)obj;
  22. }
  23. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  24. }
  25. /**
  26. * return an Enumerated from a tagged object.
  27. *
  28. * @param obj the tagged object holding the object we want
  29. * @param explicitly true if the object is meant to be explicitly
  30. * tagged false otherwise.
  31. * @exception ArgumentException if the tagged object cannot
  32. * be converted.
  33. */
  34. public static DerEnumerated GetInstance(
  35. Asn1TaggedObject obj,
  36. bool isExplicit)
  37. {
  38. Asn1Object o = obj.GetObject();
  39. if (isExplicit || o is DerEnumerated)
  40. {
  41. return GetInstance(o);
  42. }
  43. return FromOctetString(((Asn1OctetString)o).GetOctets());
  44. }
  45. public DerEnumerated(
  46. int val)
  47. {
  48. bytes = BigInteger.ValueOf(val).ToByteArray();
  49. }
  50. public DerEnumerated(
  51. BigInteger val)
  52. {
  53. bytes = val.ToByteArray();
  54. }
  55. public DerEnumerated(
  56. byte[] bytes)
  57. {
  58. this.bytes = bytes;
  59. }
  60. public BigInteger Value
  61. {
  62. get { return new BigInteger(bytes); }
  63. }
  64. internal override void Encode(
  65. DerOutputStream derOut)
  66. {
  67. derOut.WriteEncoded(Asn1Tags.Enumerated, bytes);
  68. }
  69. protected override bool Asn1Equals(
  70. Asn1Object asn1Object)
  71. {
  72. DerEnumerated other = asn1Object as DerEnumerated;
  73. if (other == null)
  74. return false;
  75. return Arrays.AreEqual(this.bytes, other.bytes);
  76. }
  77. protected override int Asn1GetHashCode()
  78. {
  79. return Arrays.GetHashCode(bytes);
  80. }
  81. private static readonly DerEnumerated[] cache = new DerEnumerated[12];
  82. internal static DerEnumerated FromOctetString(byte[] enc)
  83. {
  84. if (enc.Length == 0)
  85. {
  86. throw new ArgumentException("ENUMERATED has zero length", "enc");
  87. }
  88. if (enc.Length == 1)
  89. {
  90. int value = enc[0];
  91. if (value < cache.Length)
  92. {
  93. DerEnumerated cached = cache[value];
  94. if (cached != null)
  95. {
  96. return cached;
  97. }
  98. return cache[value] = new DerEnumerated(Arrays.Clone(enc));
  99. }
  100. }
  101. return new DerEnumerated(Arrays.Clone(enc));
  102. }
  103. }
  104. }
  105. #endif