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;
            }
        }