public void GetWindowsRuntimeBuffer_EmptyStream_ThrowsArgumentException()
 {
     using (var stream = new MemoryStream())
     {
         Assert.Throws <ArgumentException>(null, () => WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(stream, 0, 0));
     }
 }
        public void GetWindowsRuntimeBuffer_NonWritableStream_ThrowsUnauthorizedAccessException()
        {
            var memoryStream = new MemoryStream(new byte[10], false);

            Assert.Throws <UnauthorizedAccessException>(() => WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream));
            Assert.Throws <UnauthorizedAccessException>(() => WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, 0, 0));
        }
        public void GetWindowsRuntimeBuffer_Stream_Success(byte[] bytes, int positionInStream, int length)
        {
            using (var stream = new MemoryStream())
            {
                stream.Write(bytes, 0, bytes.Length);
                stream.Position = 0;

                if (positionInStream == 0 && length == bytes.Length)
                {
                    Verify(WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(stream), bytes, positionInStream, length, stream.Capacity);
                }

                stream.Position = 0;
                Verify(WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(stream, positionInStream, length), bytes, positionInStream, length, length);
            }
        }
Beispiel #4
0
        public static IBuffer ToBuffer(this IRandomAccessStream randomStream)
        {
            Stream stream       = WindowsRuntimeStreamExtensions.AsStreamForRead(randomStream.GetInputStreamAt(0));
            var    memoryStream = new MemoryStream();

            if (stream != null)
            {
                byte[] bytes = stream.ToByteArray();
                if (bytes != null)
                {
                    var binaryWriter = new BinaryWriter(memoryStream);
                    binaryWriter.Write(bytes);
                }
            }
            return(WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, 0, (int)memoryStream.Length));
        }
Beispiel #5
0
        public static IBuffer Stream2Buffer(Stream stream)
        {
            MemoryStream memoryStream = new MemoryStream();

            if (stream != null)
            {
                byte[] bytes = Stream2Bytes(stream);
                if (bytes != null)
                {
                    var binaryWriter = new BinaryWriter(memoryStream);
                    binaryWriter.Write(bytes);
                }
            }
            IBuffer buffer = WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, 0, (int)memoryStream.Length);

            return(buffer);
        }
Beispiel #6
0
        public static IBuffer RandomAccessStream2Buffer(IRandomAccessStream randomStream)
        {
            Stream       stream       = WindowsRuntimeStreamExtensions.AsStreamForRead(randomStream.GetInputStreamAt(0));
            MemoryStream memoryStream = new MemoryStream();

            if (stream != null)
            {
                byte[] bytes = Stream2Bytes(stream);
                if (bytes != null)
                {
                    var binaryWriter = new BinaryWriter(memoryStream);
                    binaryWriter.Write(bytes);
                }
            }
            IBuffer buffer = WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, 0, (int)memoryStream.Length);

            return(buffer);
        }
        /// <summary>
        /// Read a stream and return it's MD5, does not reset or close the stream.
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static string GetMD5(this Stream stream)
        {
#if NETFX_CORE
            var alg = HashAlgorithmProvider.OpenAlgorithm("MD5");

            IBuffer buff;
            if (stream is MemoryStream)
            {
                buff = WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(stream as MemoryStream);
            }
            else //In case it returned a non-Memory stream.
            {
                MemoryStream ms = new MemoryStream();
                CopyStream(stream, ms);
                buff = WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(ms);
            }
            var hashed = alg.HashData(buff);
            return(CryptographicBuffer.EncodeToHexString(hashed));
#else
            MD5CryptoServiceProvider csp = new MD5CryptoServiceProvider();
            byte[] hash = csp.ComputeHash(stream);
            return(BitConverter.ToString(hash).Replace("-", "").ToLower());
#endif
        }
Beispiel #8
0
        /// <inheritdoc />
        public override void ForwardProcessDataStream(System.IO.Stream inStream, System.IO.Stream outStream, Dictionary <string, string> options, out long writtenBytes)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            else if (!options.ContainsKey(PasswordOption))
            {
                throw new ArgumentException("Options must contain encryption key", "options");
            }

            if (outStream == null)
            {
                throw new ArgumentNullException("outStream");
            }

#if NETFX_CORE
            inStream.Seek(0, 0);
            outStream.Seek(0, 0);

            IBuffer pwBuffer   = CryptographicBuffer.ConvertStringToBinary(options[PasswordOption], BinaryStringEncoding.Utf8);
            IBuffer saltBuffer = CryptographicBuffer.CreateFromByteArray(SALT);

            // Derive key material for password size 32 bytes for AES256 algorithm
            KeyDerivationAlgorithmProvider keyDerivationProvider = Windows.Security.Cryptography.Core.KeyDerivationAlgorithmProvider.OpenAlgorithm("PBKDF2_SHA1");
            // using salt and 1000 iterations
            KeyDerivationParameters pbkdf2Parms = KeyDerivationParameters.BuildForPbkdf2(saltBuffer, 1000);

            // create a key based on original key and derivation parmaters
            CryptographicKey keyOriginal  = keyDerivationProvider.CreateKey(pwBuffer);
            IBuffer          keyMaterial  = CryptographicEngine.DeriveKeyMaterial(keyOriginal, pbkdf2Parms, 32);
            CryptographicKey derivedPwKey = keyDerivationProvider.CreateKey(pwBuffer);

            // derive buffer to be used for encryption salt from derived password key
            IBuffer saltMaterial = CryptographicEngine.DeriveKeyMaterial(derivedPwKey, pbkdf2Parms, 16);

            // display the buffers - because KeyDerivationProvider always gets cleared after each use, they are very similar unforunately
            string keyMaterialString  = CryptographicBuffer.EncodeToBase64String(keyMaterial);
            string saltMaterialString = CryptographicBuffer.EncodeToBase64String(saltMaterial);

            SymmetricKeyAlgorithmProvider symProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm("AES_CBC_PKCS7");
            // create symmetric key from derived password key
            CryptographicKey symmKey = symProvider.CreateSymmetricKey(keyMaterial);

            using (MemoryStream ms = new MemoryStream())
            {
                inStream.CopyTo(ms);
                // encrypt data buffer using symmetric key and derived salt material
                IBuffer resultBuffer = CryptographicEngine.Encrypt(symmKey, WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(ms), saltMaterial);
                resultBuffer.AsStream().CopyTo(outStream);
                writtenBytes = outStream.Position;
            }
#else
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(options[PasswordOption], SALT);
            var key = pdb.GetBytes(32);
            pdb.Reset();
            var iv = pdb.GetBytes(16);

            using (var transform = encrypter.CreateEncryptor(key, iv))
            {
                using (MemoryStream internalStream = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(internalStream, transform, CryptoStreamMode.Write))
                    {
                        StreamTools.Write(inStream, csEncrypt);
                        inStream.Flush();
                        csEncrypt.FlushFinalBlock();

                        internalStream.Seek(0, 0);
                        StreamTools.Write(internalStream, outStream);
                        writtenBytes = outStream.Position;
                    }
                }
            }
#endif
        }
        public void GetWindowsRuntimeBuffer_BufferWithLengthGreaterThanIntMax_Throws()
        {
            var memoryStream = new SubMemoryStream(10);

            WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, 0, 0);
        }
        public void GetWindowsRuntimeBuffer_PositonInStreamGreaterOrEqualToCapacity_ThrowsArgumentException(int positionInStream)
        {
            var memoryStream = new MemoryStream(1);

            AssertExtensions.Throws <ArgumentException>(null, () => WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, positionInStream, 0));
        }
        public void GetWindowsRuntimeBuffer_NegativeLength_ThrowsArgumentOufOfRangeException()
        {
            var memoryStream = new MemoryStream();

            AssertExtensions.Throws <ArgumentOutOfRangeException>("length", () => WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, 0, -1));
        }
        public void GetWindowsRuntimeBuffer_NegativePositionInStream_ThrowsArgumentOufOfRangeException()
        {
            var memoryStream = new MemoryStream();

            AssertExtensions.Throws <ArgumentOutOfRangeException>("positionInStream", () => WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, -1, 0));
        }
 public void GetWindowsRuntimeBuffer_NullStream_ThrowsArgumentNullException()
 {
     AssertExtensions.Throws <ArgumentNullException>("underlyingStream", () => WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(null));
     AssertExtensions.Throws <ArgumentNullException>("underlyingStream", () => WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(null, 0, 0));
 }