public void WriteToStreamAndVerify()
            {
                var path = Env.ResolvePath("~/Resources/NewDatabase23.kdbx");
                if (System.IO.File.Exists(path))
                    System.IO.File.Delete(path);

                try {
                    var key = new MasterKey();
                    key.Add(new MasterKeyPassword("megatron")); // terrible pw. 
                    var package = new KeePassPackage(key);

                    using (var fs = System.IO.File.OpenWrite(path))
                    {
                        var group = new Group();
                        group.Name = "Sample";

                        package.Root.Groups.Add(group);

                        var entry = new Entry(true);
                        var fields = entry.AsEntryFields();
                        fields.Title = "Test";
                        fields.Password = "******";
                        group.Entries.Add(entry);

                        package.Save(fs);


                        using (var fs2 = System.IO.File.OpenRead(Env.ResolvePath("~/Resources/NewDatabase23.kdbx")))
                        {
                            var package2 = new KeePassPackage(key);
                            package2.Open(fs2);

                            Assert.NotNull(package2);
                            Assert.NotNull(package2.Root);
                            Assert.NotNull(package2.Root.Groups);
                            Assert.Equal(1, package2.Root.Groups.Count);
                            Assert.NotNull(package2.Root.Groups[0].Entries);
                            Assert.Equal(1, package2.Root.Groups[0].Entries.Count);

                            var entry2 = package2.Root.Groups[0].Entries[0];
                            var titleString = entry2.Strings.SingleOrDefault(o => o.Key == "Title");
                            var passwordString = entry2.Strings.SingleOrDefault(o => o.Key == "Password");
                            Assert.Equal("Test", titleString.Value.Unprotect());
                            Assert.Equal("Password", passwordString.Value.Unprotect());
                        }
                    }
                } 
                finally
                {
                    //if (System.IO.File.Exists(path))
                    //    System.IO.File.Delete(path);
                }
            }
            public void OpenStreamAndRead()
            {
                var key = new MasterKey();
                key.Add(new MasterKeyPassword("password")); // terrible pw. 
                var package = new KeePassPackage(key);

                Assert.Null(package.Meta.DatabaseName);

                using (var fs = System.IO.File.OpenRead(Env.ResolvePath("~/Resources/NewDatabase.kdbx")))
                
                {
                    package.Open(fs);
                    var meta = package.Meta;

                    var predicates = new List<Func<IEntryFields, bool>>();
                    var matches = new List<Entry>();

                    if (!string.IsNullOrWhiteSpace(this.Title))
                        predicates.Add(this.CreateSeelctor("Title", this.Title, false));




                    if (package.Root != null && package.Root.Groups != null)
                    {
                        foreach (var group in package.Root.Groups)
                        {
                            if (!Collect(group, predicates, matches))
                            {
                                break;
                            }
                        }
                    }

                    Assert.NotEmpty(matches);
                    Assert.Equal("Password", matches[0].AsEntryFields().Password);
                    foreach(var match in matches)
                    {
                        var t = match.AsEntryFields().Title;
                        var v = match.AsEntryFields().Password;
                        if(v == null)
                            Console.WriteLine("{0} {1} null", t, v);
                        else 
                            Console.WriteLine("{0} {1}", t, v);

                       
                    }
                }
            }
        private static void Save(KeePassPackage package, Stream stream)
        {
            package.HeaderInfo.GenerateValues();
            var actualByteMarks = package.HeaderInfo.HeaderByteMarks;
            package.HeaderInfo.Write(stream);
            var outerStream = KeePassFileCryptoStreamFactory.CreateCryptoStream(
                stream,
                true,
                package.MasterKey,
                package.HeaderInfo);

           
            outerStream.Write(actualByteMarks, 0, actualByteMarks.Length);

            outerStream = new HMACBlockStream(outerStream, true);

            if (package.HeaderInfo.DatabaseCompression == 1)
                outerStream = new GZipStream(outerStream, CompressionMode.Compress);

            switch(package.Format)
            {
                case DocumentFormats.Xml:
                    var serializer = new Xml.KeePassPackageXmlSerializer();
                    serializer.Write(package, outerStream);
                    break;
            }

            outerStream.Dispose();
        }
        private static void Open(KeePassPackage package, Stream stream)
        {
            package.HeaderInfo.Read(stream);
            var outerStream = KeePassFileCryptoStreamFactory.CreateCryptoStream(
                stream,
                false,
                package.MasterKey,
                package.HeaderInfo);

            byte[] expectedHeaderByteMark = package.HeaderInfo.HeaderByteMarks;
            byte[] actualHeaderByteMark = outerStream.ReadBytes(32);

            
            bool equal = Check.Equal(expectedHeaderByteMark, actualHeaderByteMark);
            if(!equal)
                throw new Exception("Invalid File Format");

            outerStream = new HMACBlockStream(outerStream, false);

            if(package.HeaderInfo.DatabaseCompression == 1)
               outerStream = new GZipStream(outerStream, CompressionMode.Decompress);

            switch(package.Format)
            {
                case DocumentFormats.Xml:
                    var serializer = new Xml.KeePassPackageXmlSerializer();
                    serializer.Read(package, outerStream);
                    break;
            }
        }