Exemplo n.º 1
0
        public virtual int Write(BufRefLen src)
        {
            PreWriteCheck(src.Length);

            Array.Copy(src.BaseArray, src.BaseArrayOffset, BaseArray, BaseArrayOffset, src.Length);
            Seek(src.Length);
            src.Seek(src.Length);
            return(src.Length);
        }
Exemplo n.º 2
0
        public static BufLen Decrypt(BufLen data, I2PPrivateKey pkey, bool zeropad)
        {
            if (data == null || (zeropad && data.Length != 514))
            {
                throw new ArgumentException("ElGamal padded data to decrypt must be exactly 514 bytes!");
            }
            if (!zeropad && data.Length != 512)
            {
                throw new ArgumentException("ElGamal data to decrypt must be exactly 512 bytes!");
            }

            var x = I2PConstants.ElGamalP.Subtract(pkey.ToBigInteger()).Subtract(BigInteger.One);

            BigInteger a, b;
            var        reader = new BufRefLen(data);

            if (zeropad)
            {
                reader.Seek(1);
                a = reader.ReadBigInteger(256);
                reader.Seek(1);
                b = reader.ReadBigInteger(256);
            }
            else
            {
                a = reader.ReadBigInteger(256);
                b = reader.ReadBigInteger(256);
            }

            var m2   = b.Multiply(a.ModPow(x, I2PConstants.ElGamalP));
            var m1   = m2.Mod(I2PConstants.ElGamalP);
            var m    = m1.ToByteArray(255);
            var hash = I2PHashSHA256.GetHash(m, 33, 222);

            if (!BufUtils.Equal(m, 1, hash, 0, 32))
            {
                throw new HashCheckFailException();
            }

            return(new BufLen(m, 33, 222));
        }
Exemplo n.º 3
0
        public static byte[] BCGZipDecompress(BufLen buf)
        {
            var reader = new BufRefLen(buf);

            using (var ms = new MemoryStream())
            {
                // Skip gzip header
                var gzheader = reader.ReadBufLen(10);
                var flag     = gzheader.Peek8(3);
                if ((flag & 0x04) != 0)
                {
                    reader.Seek(reader.Read16());                           // "Extra"
                }
                if ((flag & 0x08) != 0)
                {
                    while (reader.Read8() != 0)
                    {
                        ;                                                   // "Name"
                    }
                }
                if ((flag & 0x10) != 0)
                {
                    while (reader.Read8() != 0)
                    {
                        ;                                                   // "Comment"
                    }
                }
                if ((flag & 0x02) != 0)
                {
                    reader.Read16();                         // "CRC16"
                }
                ms.Write(reader.BaseArray, reader.BaseArrayOffset, reader.Length);
                ms.Position = 0;

                using (var gzs = new ZInputStream(ms, true))
                {
                    var gzdata = StreamUtils.Read(gzs);
                    return(gzdata);
                }
            }
        }
Exemplo n.º 4
0
        public static BufLen BCGZipDecompressNew(BufLen buf)
        {
            var reader = new BufRefLen(buf);

            // Skip gzip header
            var gzheader = reader.ReadBufLen(10);
            var flag     = gzheader.Peek8(3);

            if ((flag & 0x04) != 0)
            {
                reader.Seek(reader.Read16());                           // "Extra"
            }
            if ((flag & 0x08) != 0)
            {
                while (reader.Read8() != 0)
                {
                    ;                                                   // "Name"
                }
            }
            if ((flag & 0x10) != 0)
            {
                while (reader.Read8() != 0)
                {
                    ;                                                   // "Comment"
                }
            }
            if ((flag & 0x02) != 0)
            {
                reader.Read16();                         // "CRC16"
            }
            var z = new ZStream();
            z.inflateInit(true);

            var dest   = new byte[buf.Length * 2];
            var destix = 0;

            z.next_in_index = reader.BaseArrayOffset;
            z.next_in       = reader.BaseArray;
            z.avail_in      = reader.Length - 8;

bigger_dest:

            z.next_out       = dest;
            z.next_out_index = destix;
            z.avail_out      = dest.Length - destix;
            var err = z.inflate(JZlib.Z_FINISH);
            if (err != JZlib.Z_BUF_ERROR && err != JZlib.Z_OK && err != JZlib.Z_STREAM_END)
            {
                throw new IOException("inflating: " + z.msg);
            }

            if (z.avail_out == 0)
            {
                var newdest = new byte[dest.Length * 2];
                Array.Copy(dest, newdest, dest.Length);
                destix = dest.Length;
                dest   = newdest;
                goto bigger_dest;
            }

            var result = new BufLen(dest, 0, dest.Length - z.avail_out);
            z.inflateEnd();

            return(result);
        }