Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PbdfFile"/> class from the given <paramref name="stream"/>.
        /// </summary>
        /// <param name="stream">The <see cref="Stream"/> to load data from.</param>
        public PbdfFile(Stream stream, object parameter = null)
        {
            Parameter = parameter;

            // Retrieve encryption secrets.
            Key             = Pbdf.RetrieveKey(stream);
            stream.Position = 0;
            BlockSize       = Pbdf.RetrieveBlockSize(stream, Key);
            stream.Position = 0;

            // Decrypt header and data into a temporary stream.
            byte[] buffer = new byte[stream.Length - (stream.Length / BlockSize * sizeof(uint))];
            using (MemoryStream decStream = new MemoryStream(buffer))
            {
                Pbdf.Decrypt(stream, decStream, Key, BlockSize);

                // Read the header and adjusted offsets.
                decStream.Position = 0;
                Offsets            = Pbdf.ReadHeader(decStream, BlockSize);
                int headerSize = (int)decStream.Position;

                // Create a stream view on the data excluding the header.
                using (MemoryStream dataStream = new MemoryStream(buffer, headerSize, buffer.Length - headerSize))
                    LoadData(dataStream);
            }
        }
Beispiel #2
0
 /// <summary>
 /// Saves the data of the instance in the given <paramref name="stream"/>.
 /// </summary>
 /// <param name="stream">The <see cref="Stream"/> to save data in.</param>
 public void Save(Stream stream)
 {
     // Compose the decrypted file contents in a temporary stream.
     using (MemoryStream decStream = new MemoryStream())
     {
         // Write out the file data into a temporary stream.
         Offsets = new List <int>();
         using (MemoryStream dataStream = new MemoryStream())
         {
             SaveData(dataStream);
             Pbdf.WriteHeader(decStream, BlockSize, Offsets, (int)dataStream.Position);
             dataStream.Position = 0;
             dataStream.CopyTo(decStream);
         }
         // Encrypt the data into the provided stream.
         decStream.Position = 0;
         Pbdf.Encrypt(decStream, stream, Key, BlockSize);
     }
 }