MPInteger
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Math.EC;
using Org.BouncyCastle.Utilities;
using System;
using System.IO;
namespace Org.BouncyCastle.Bcpg
{
public sealed class MPInteger : BcpgObject
{
private readonly BigInteger m_value;
public BigInteger Value => m_value;
public MPInteger(BcpgInputStream bcpgIn)
{
if (bcpgIn == null)
throw new ArgumentNullException("bcpgIn");
bool validateLength = false;
m_value = ReadMpi(bcpgIn, validateLength);
}
public MPInteger(BigInteger val)
{
if (val == null)
throw new ArgumentNullException("val");
if (val.SignValue < 0)
throw new ArgumentException("Values must be positive", "val");
m_value = val;
}
public override void Encode(BcpgOutputStream bcpgOut)
{
Encode(bcpgOut, m_value);
}
internal static void Encode(BcpgOutputStream bcpgOut, BigInteger n)
{
StreamUtilities.WriteUInt16BE(bcpgOut, (ushort)n.BitLength);
BigIntegers.WriteUnsignedByteArray(bcpgOut, n);
}
internal static BigInteger ToMpiBigInteger(ECPoint point)
{
byte[] encoded = point.GetEncoded(false);
return new BigInteger(1, encoded);
}
private static BigInteger ReadMpi(BcpgInputStream bcpgIn, bool validateLength)
{
int num = StreamUtilities.RequireUInt16BE(bcpgIn);
byte[] array = new byte[(num + 7) / 8];
bcpgIn.ReadFully(array);
BigInteger bigInteger = new BigInteger(1, array);
if (validateLength && bigInteger.BitLength != num)
throw new IOException("malformed MPI");
return bigInteger;
}
}
}