MPInteger.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Bcpg
  7. {
  8. /// <remarks>A multiple precision integer</remarks>
  9. public class MPInteger
  10. : BcpgObject
  11. {
  12. private readonly BigInteger val;
  13. public MPInteger(
  14. BcpgInputStream bcpgIn)
  15. {
  16. if (bcpgIn == null)
  17. throw new ArgumentNullException("bcpgIn");
  18. int length = (bcpgIn.ReadByte() << 8) | bcpgIn.ReadByte();
  19. byte[] bytes = new byte[(length + 7) / 8];
  20. bcpgIn.ReadFully(bytes);
  21. this.val = new BigInteger(1, bytes);
  22. }
  23. public MPInteger(
  24. BigInteger val)
  25. {
  26. if (val == null)
  27. throw new ArgumentNullException("val");
  28. if (val.SignValue < 0)
  29. throw new ArgumentException("Values must be positive", "val");
  30. this.val = val;
  31. }
  32. public BigInteger Value
  33. {
  34. get { return val; }
  35. }
  36. public override void Encode(
  37. BcpgOutputStream bcpgOut)
  38. {
  39. bcpgOut.WriteShort((short) val.BitLength);
  40. bcpgOut.Write(val.ToByteArrayUnsigned());
  41. }
  42. internal static void Encode(
  43. BcpgOutputStream bcpgOut,
  44. BigInteger val)
  45. {
  46. bcpgOut.WriteShort((short) val.BitLength);
  47. bcpgOut.Write(val.ToByteArrayUnsigned());
  48. }
  49. }
  50. }
  51. #pragma warning restore
  52. #endif