DerInteger.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 DerInteger
  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 DerInteger GetInstance(
  17. object obj)
  18. {
  19. if (obj == null || obj is DerInteger)
  20. {
  21. return (DerInteger)obj;
  22. }
  23. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  24. }
  25. /**
  26. * return an Integer from a tagged object.
  27. *
  28. * @param obj the tagged object holding the object we want
  29. * @param isExplicit 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 DerInteger GetInstance(
  35. Asn1TaggedObject obj,
  36. bool isExplicit)
  37. {
  38. if (obj == null)
  39. throw new ArgumentNullException("obj");
  40. Asn1Object o = obj.GetObject();
  41. if (isExplicit || o is DerInteger)
  42. {
  43. return GetInstance(o);
  44. }
  45. return new DerInteger(Asn1OctetString.GetInstance(o).GetOctets());
  46. }
  47. public DerInteger(
  48. int value)
  49. {
  50. bytes = BigInteger.ValueOf(value).ToByteArray();
  51. }
  52. public DerInteger(
  53. BigInteger value)
  54. {
  55. if (value == null)
  56. throw new ArgumentNullException("value");
  57. bytes = value.ToByteArray();
  58. }
  59. public DerInteger(
  60. byte[] bytes)
  61. {
  62. this.bytes = bytes;
  63. }
  64. public BigInteger Value
  65. {
  66. get { return new BigInteger(bytes); }
  67. }
  68. /**
  69. * in some cases positive values Get crammed into a space,
  70. * that's not quite big enough...
  71. */
  72. public BigInteger PositiveValue
  73. {
  74. get { return new BigInteger(1, bytes); }
  75. }
  76. internal override void Encode(
  77. DerOutputStream derOut)
  78. {
  79. derOut.WriteEncoded(Asn1Tags.Integer, bytes);
  80. }
  81. protected override int Asn1GetHashCode()
  82. {
  83. return Arrays.GetHashCode(bytes);
  84. }
  85. protected override bool Asn1Equals(
  86. Asn1Object asn1Object)
  87. {
  88. DerInteger other = asn1Object as DerInteger;
  89. if (other == null)
  90. return false;
  91. return Arrays.AreEqual(this.bytes, other.bytes);
  92. }
  93. public override string ToString()
  94. {
  95. return Value.ToString();
  96. }
  97. }
  98. }
  99. #endif