예제 #1
0
        public BigInteger DecodeValue(Asn1DecodeBuffer buffer, int length)
        {
            var ivalue = new byte[length];

            if (length > MaxBigIntLen)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1TooBigIntegerValue, length);
            }

            for (var i = 0; i < length; ++i)
            {
                ivalue[i] = (byte)buffer.ReadByte();
            }

            var integer = new BigInteger();

            if (length > 0)
            {
                integer.SetData(ivalue);
            }

            return integer;
        }
예제 #2
0
 public Asn1BigInteger(BigInteger value)
 {
     _value = value;
 }
예제 #3
0
 public Asn1BigInteger()
 {
     _value = new BigInteger();
 }
예제 #4
0
        private BigInteger GetCopyAndInverse()
        {
            var integer = new BigInteger();

            if (_value.Length > 0)
            {
                integer._value = new byte[_value.Length];

                if (_sign < 0)
                {
                    integer._value = GetData();
                    integer._sign = 1;
                    return integer;
                }

                Array.Copy(_value, 0, integer._value, 0, _value.Length);
                integer._sign = _sign;

                return integer;
            }

            integer._value = Zero;

            return integer;
        }
예제 #5
0
        private BigInteger GetCopy()
        {
            var integer = new BigInteger();

            if (_value.Length > 0)
            {
                integer._value = new byte[_value.Length];
                Array.Copy(_value, 0, integer._value, 0, _value.Length);
            }
            else
            {
                integer._value = Zero;
            }

            integer._sign = _sign;

            return integer;
        }
예제 #6
0
        private static int ShiftLeft(BigInteger data, uint shift)
        {
            var value = data._value;
            var length = value.Length;
            var index = (int)(shift >> AddressBits);
            var num3 = ((int)shift) & BitIndexMask;
            var num4 = 8 - num3;
            var num5 = 0;
            var num7 = length;

            if (length != 0)
            {
                length = length << AddressBits;
                var num6 = (int)((((length - shift) + 8L) - 1L) >> AddressBits);

                while (num5 < (num6 - 1))
                {
                    value[num5++] = (byte)((value[index] << num3) | ((num4 == 8) ? 0 : (value[index + 1] >> num4)));
                    index++;
                }

                length &= BitIndexMask;
                value[num5] = (num7 == num6) ? ((byte)((value[index] & BitsLeftOf(length)) << num3)) : ((byte)((value[index] << num3) | ((num4 == 8) ? 0 : ((value[index + 1] & BitsLeftOf(length)) >> num4))));

                if (num6 < num7)
                {
                    for (var i = num6; i < (num7 - num6); i++)
                    {
                        value[i] = 0;
                    }
                }
            }

            return 0;
        }
예제 #7
0
 private static void FastCopy(ref BigInteger src, ref BigInteger dst)
 {
     dst._value = new byte[src._value.Length];
     Array.Copy(src._value, 0, dst._value, 0, src._value.Length);
     dst._sign = src._sign;
 }
예제 #8
0
 public Asn1BigInteger()
 {
     _value = new BigInteger();
 }
예제 #9
0
        public string ToString(int radix)
        {
            if ((radix == 2) || (radix == 0x10))
            {
                int num;
                int num2;

                if (radix == 2)
                {
                    num2 = 8;
                    num = 1;
                }
                else
                {
                    num2 = 2;
                    num = 4;
                }

                var num3 = num2 * GetDataLen();
                var chArray = new char[num3];
                var index = num3 - 1;

                for (var i = _value.Length - 1; i >= 0; i--)
                {
                    byte num6;
                    int num8;

                    if (_sign < 0)
                    {
                        unchecked
                        {
                            num6 = (byte)~_value[i];
                        }

                        if ((_sign < 0) && ((num6 = (byte)(num6 + 1)) != 0))
                        {
                            _sign = 0;
                        }
                    }
                    else
                    {
                        num6 = _value[i];
                    }

                    var num7 = num8 = 0;

                    while (num7 < num2)
                    {
                        var b = (num6 >> num8) & ((1 << num) - 1);
                        chArray[index] = NibbleToHexChar(b);
                        num7++;
                        index--;
                        num8 += num;
                    }
                }

                while (index >= 0)
                {
                    chArray[index--] = '0';
                }

                return new string(chArray);
            }

            var reminder = 0;
            var str = "";
            var quotient = new BigInteger();

            var copy = (radix == 10) ? GetCopy() : GetCopyAndInverse();

            do
            {
                DivideByInt(ref copy, ByteRadix[radix], ref quotient, ref reminder);
                var str2 = IntToStr(reminder, radix);
                var length = str2.Length;

                str = str2 + str;

                if ((quotient._value.Length != 0) || (radix != 10))
                {
                    int num12;

                    for (num12 = length; num12 < DigitsPerByte[radix]; num12++)
                    {
                        str = '0' + str;
                    }

                    FastCopy(ref quotient, ref copy);

                    if (((quotient._value.Length == 0) && (_sign > 0)) && ((radix != 10) && ((reminder & 0x80) != 0)))
                    {
                        str = '0' + str;
                    }
                }
                else if ((_sign < 0) && (radix == 10))
                {
                    str = '-' + str;
                }

            }
            while ((quotient._value != null) && (quotient._value.Length != 0));

            return str;
        }
예제 #10
0
 public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
 {
     var length = explicitTagging ? MatchTag(buffer, Tag) : implicitLength;
     _value = DecodeValue(buffer, length);
     buffer.TypeCode = 2;
 }
예제 #11
0
 public Asn1BigInteger(string value, int radix)
 {
     _value = new BigInteger(value, radix);
 }
예제 #12
0
 public Asn1BigInteger(string value)
 {
     _value = new BigInteger(value);
 }
예제 #13
0
 public Asn1BigInteger(BigInteger value)
 {
     _value = value;
 }
예제 #14
0
 public Asn1BigInteger(string value)
 {
     _value = new BigInteger(value);
 }
예제 #15
0
        private static void DivideByInt(ref BigInteger divident, int divisor, ref BigInteger quotient, ref int reminder)
        {
            var index = 0;
            var num3 = 4;
            var num4 = 0;
            var num5 = 0;

            if (divisor == 0)
            {
                return;
            }

            reminder = 0;

            if (divident._sign == 0)
            {
                quotient._sign = 0;
                quotient._value = Zero;
                return;
            }

            quotient._value = new byte[divident._value.Length];

            var num2 = quotient._value.Length - 1;
            quotient._sign = ((quotient._sign * divisor) > 0) ? 1 : -1;

            var num6 = divident._value.Length * 2;

            while (num4 < num6)
            {
                num5 = num5 << 4;
                num4++;
                num5 |= (divident._value[index] >> num3) & 15;

                if (num3 == 0)
                {
                    num3 = 4;
                    index++;
                }
                else
                {
                    num3 = 0;
                }

                ShiftLeft(quotient, 4);

                if (num5 >= divisor)
                {
                    quotient._value[num2] = (byte)(quotient._value[num2] | ((byte)((num5 / divisor) & 15)));
                    num5 = num5 % divisor;
                }

                reminder = num5;
            }

            quotient._value = RemoveLeadingZeroBytes(quotient._value);
        }
예제 #16
0
 public Asn1BigInteger(string value, int radix)
 {
     _value = new BigInteger(value, radix);
 }
예제 #17
0
        private static int EncodeValue(Asn1EncodeBuffer buffer, BigInteger ivalue, bool doCopy)
        {
            var data = ivalue.GetData();
            var length = data.Length;

            for (var i = length - 1; i >= 0; --i)
            {
                if (doCopy)
                {
                    buffer.Copy(data[i]);
                }
            }

            return length;
        }