Beispiel #1
0
        public static long GetLong(Stream stream)
        {
            if (stream.Length != 8)
            {
                throw new ArgumentException();
            }

            byte[] buffer = null;

            try
            {
                buffer = _bufferManager.TakeBuffer(8);

                stream.Read(buffer, 0, 8);

                return(NetworkConverter.ToInt64(buffer));
            }
            finally
            {
                if (buffer != null)
                {
                    _bufferManager.ReturnBuffer(buffer);
                }
            }
        }
Beispiel #2
0
        public void Test_NetworkConverter()
        {
            Assert.IsTrue(NetworkConverter.ToHexString(new byte[] { 0x00, 0x9e, 0x0f }) == "009e0f", "ToHexString");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.FromHexString("1af4b"), new byte[] { 0x01, 0xaf, 0x4b }), "FromHexString");

            Assert.IsTrue(NetworkConverter.ToBoolean(new byte[] { 0x01 }), "ToBoolean");
            Assert.IsTrue(NetworkConverter.ToChar(new byte[] { 0x00, 0x41 }) == 'A', "ToChar");
            Assert.IsTrue(NetworkConverter.ToInt16(new byte[] { 0x74, 0xab }) == 0x74ab, "ToInt16");
            Assert.IsTrue(NetworkConverter.ToUInt16(new byte[] { 0x74, 0xab }) == 0x74ab, "ToUInt16");
            Assert.IsTrue(NetworkConverter.ToInt32(new byte[] { 0x74, 0xab, 0x05, 0xc1 }) == 0x74ab05c1, "ToInt32");
            Assert.IsTrue(NetworkConverter.ToUInt32(new byte[] { 0x74, 0xab, 0x05, 0xc1 }) == 0x74ab05c1, "ToUInt32");
            Assert.IsTrue(NetworkConverter.ToInt64(new byte[] { 0x74, 0xab, 0xa5, 0xbb, 0xf5, 0x39, 0x7b, 0x15 }) == 0x74aba5bbf5397b15, "ToInt64");
            Assert.IsTrue(NetworkConverter.ToUInt64(new byte[] { 0x74, 0xab, 0xa5, 0xbb, 0xf5, 0x39, 0x7b, 0x15 }) == 0x74aba5bbf5397b15, "ToUInt64");
            Assert.IsTrue(NetworkConverter.ToSingle(new byte[] { 0x4a, 0x8a, 0xd0, 0x64 }) == 4548658.0, "ToSingle");
            Assert.IsTrue(NetworkConverter.ToDouble(new byte[] { 0x41, 0xb8, 0xa6, 0xb9, 0x83, 0x27, 0x97, 0xa3 }) == 413579651.15465754, "ToDouble");

            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes(true), new byte[] { 0x01 }), "GetBytes #bool");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((char)'A'), new byte[] { 0x00, 0x41 }), "GetBytes #char");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((short)0x74ab), new byte[] { 0x74, 0xab }), "GetBytes #short");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((ushort)0x74ab), new byte[] { 0x74, 0xab }), "GetBytes #ushort");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((int)0x74ab05c1), new byte[] { 0x74, 0xab, 0x05, 0xc1 }), "GetBytes #int");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((uint)0x74ab05c1), new byte[] { 0x74, 0xab, 0x05, 0xc1 }), "GetBytes #uint");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((long)0x74aba5bbf5397b15), new byte[] { 0x74, 0xab, 0xa5, 0xbb, 0xf5, 0x39, 0x7b, 0x15 }), "GetBytes #long");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((ulong)0x74aba5bbf5397b15), new byte[] { 0x74, 0xab, 0xa5, 0xbb, 0xf5, 0x39, 0x7b, 0x15 }), "GetBytes #ulong");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((float)4548658.0), new byte[] { 0x4a, 0x8a, 0xd0, 0x64 }), "GetBytes #float");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((double)413579651.15465754), new byte[] { 0x41, 0xb8, 0xa6, 0xb9, 0x83, 0x27, 0x97, 0xa3 }), "GetBytes #double");

            Assert.IsTrue(NetworkConverter.ToInt32(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x74, 0xab, 0x05, 0xc1 }, 4) == 0x74ab05c1, "ToInt32");

            for (int i = 0; i < 1024; i++)
            {
                byte[] buffer = new byte[_random.Next(0, 128)];
                _random.NextBytes(buffer);

                var s = NetworkConverter.ToBase64UrlString(buffer);
                Assert.IsTrue(CollectionUtilities.Equals(buffer, NetworkConverter.FromBase64UrlString(s)));
            }

            for (int i = 0; i < 1024; i++)
            {
                byte[] buffer = new byte[_random.Next(0, 128)];
                _random.NextBytes(buffer);

                var s = NetworkConverter.ToHexString(buffer);
                Assert.IsTrue(CollectionUtilities.Equals(buffer, NetworkConverter.FromHexString(s)));
            }
        }
Beispiel #3
0
        public override Stream Receive(TimeSpan timeout, Information options)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            if (!_connect)
            {
                throw new ConnectionException();
            }

            lock (_receiveLock)
            {
                try
                {
                    if (_version.HasFlag(SecureConnectionVersion.Version3))
                    {
                        using (Stream stream = _connection.Receive(timeout, options))
                        {
                            byte[] totalReceiveSizeBuff = new byte[8];
                            if (stream.Read(totalReceiveSizeBuff, 0, totalReceiveSizeBuff.Length) != totalReceiveSizeBuff.Length)
                            {
                                throw new ConnectionException();
                            }
                            long totalReceiveSize = NetworkConverter.ToInt64(totalReceiveSizeBuff);

                            if (_informationVersion3.HashAlgorithm.HasFlag(SecureVersion3.HashAlgorithm.Sha256))
                            {
                                const int hashLength = 32;

                                _totalReceiveSize += (stream.Length - (8 + hashLength));

                                if (totalReceiveSize != _totalReceiveSize)
                                {
                                    throw new ConnectionException();
                                }

                                byte[] otherHmacBuff = new byte[hashLength];
                                stream.Seek(-hashLength, SeekOrigin.End);
                                if (stream.Read(otherHmacBuff, 0, otherHmacBuff.Length) != otherHmacBuff.Length)
                                {
                                    throw new ConnectionException();
                                }
                                stream.SetLength(stream.Length - hashLength);
                                stream.Seek(0, SeekOrigin.Begin);

                                byte[] myHmacBuff = HmacSha256.ComputeHash(stream, _informationVersion3.OtherHmacKey);

                                if (!Unsafe.Equals(otherHmacBuff, myHmacBuff))
                                {
                                    throw new ConnectionException();
                                }

                                stream.Seek(8, SeekOrigin.Begin);
                            }
                            else
                            {
                                throw new ConnectionException();
                            }

                            BufferStream bufferStream = new BufferStream(_bufferManager);

                            if (_informationVersion3.CryptoAlgorithm.HasFlag(SecureVersion3.CryptoAlgorithm.Aes256))
                            {
                                byte[] iv = new byte[16];
                                stream.Read(iv, 0, iv.Length);

                                using (var aes = Aes.Create())
                                {
                                    aes.KeySize = 256;
                                    aes.Mode    = CipherMode.CBC;
                                    aes.Padding = PaddingMode.PKCS7;

                                    using (CryptoStream cs = new CryptoStream(new WrapperStream(bufferStream, true), aes.CreateDecryptor(_informationVersion3.OtherCryptoKey, iv), CryptoStreamMode.Write))
                                    {
                                        byte[] receiveBuffer = null;

                                        try
                                        {
                                            receiveBuffer = _bufferManager.TakeBuffer(1024 * 4);

                                            int i = -1;

                                            while ((i = stream.Read(receiveBuffer, 0, receiveBuffer.Length)) > 0)
                                            {
                                                cs.Write(receiveBuffer, 0, i);
                                            }
                                        }
                                        finally
                                        {
                                            if (receiveBuffer != null)
                                            {
                                                _bufferManager.ReturnBuffer(receiveBuffer);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                throw new ConnectionException();
                            }

                            bufferStream.Seek(0, SeekOrigin.Begin);
                            return(bufferStream);
                        }
                    }
                    else
                    {
                        throw new ConnectionException();
                    }
                }
                catch (ConnectionException e)
                {
                    throw e;
                }
                catch (Exception e)
                {
                    throw new ConnectionException(e.Message, e);
                }
            }
        }