Esempio n. 1
0
        public static byte[] encodeInteger(BigInteger x)
        {
            if (x < 0)
            {
                throw new ArgumentException("x cannot be negative");
            }

            string t = x.ToString("X");

            if (t.Length % 2 == 1)
            {
                t = "0" + t;
            }

            byte[] xBytes = BinaryAscii.binaryFromHex(t);

            int num = xBytes[0];

            if (num <= hex127)
            {
                return(combineByteArrays(new List <byte[]> {
                    bytesHexB,
                    Bytes.intToCharBytes(xBytes.Length),
                    xBytes
                }));
            }

            return(combineByteArrays(new List <byte[]> {
                bytesHexB,
                Bytes.intToCharBytes(xBytes.Length + 1),
                bytesHexAt,
                xBytes
            }));
        }
Esempio n. 2
0
        private static byte[] encodeLength(int length)
        {
            if (length < 0)
            {
                throw new ArgumentException("length cannot be negative");
            }

            if (length < hex128)
            {
                return(Bytes.intToCharBytes(length));
            }

            string s = length.ToString("X");

            if ((s.Length % 2) == 1)
            {
                s = "0" + s;
            }

            byte[] bytes     = BinaryAscii.binaryFromHex(s);
            int    lengthLen = bytes.Length;

            return(combineByteArrays(new List <byte[]> {
                Bytes.intToCharBytes(hex128 | lengthLen),
                bytes
            }));
        }
Esempio n. 3
0
 private static void checkSequenceError(byte[] bytes, string start, string expected)
 {
     if (BinaryAscii.hexFromBinary(bytes).Substring(0, start.Length) != start)
     {
         throw new ArgumentException(
                   "wanted sequence " +
                   expected.Substring(0, 2) +
                   ", got " +
                   extractFirstInt(bytes).ToString("X")
                   );
     }
 }
Esempio n. 4
0
        public static Tuple <BigInteger, byte[]> removeInteger(byte[] bytes)
        {
            checkSequenceError(bytes, hexB, "02");

            Tuple <int, int> readLengthResult = readLength(Bytes.sliceByteArray(bytes, 1));
            int length    = readLengthResult.Item1;
            int lengthLen = readLengthResult.Item2;

            byte[] numberBytes = Bytes.sliceByteArray(bytes, 1 + lengthLen, length);
            byte[] rest        = Bytes.sliceByteArray(bytes, 1 + lengthLen + length);
            int    nBytes      = numberBytes[0];

            if (nBytes >= hex128)
            {
                throw new ArgumentException("first byte of integer must be < 128");
            }

            return(new Tuple <BigInteger, byte[]> (
                       BinaryAscii.numberFromHex(BinaryAscii.hexFromBinary(numberBytes)),
                       rest
                       ));
        }
Esempio n. 5
0
        private static Tuple <int, int> readLength(byte[] bytes)
        {
            int num = extractFirstInt(bytes);

            if ((num & hex128) == 0)
            {
                return(new Tuple <int, int>(num & hex127, 1));
            }

            int lengthLen = num & hex127;

            if (lengthLen > bytes.Length - 1)
            {
                throw new ArgumentException("ran out of length bytes");
            }

            return(new Tuple <int, int>(
                       int.Parse(
                           BinaryAscii.hexFromBinary(Bytes.sliceByteArray(bytes, 1, lengthLen)),
                           System.Globalization.NumberStyles.HexNumber
                           ),
                       1 + lengthLen
                       ));
        }