Esempio n. 1
0
        /// <summary>
        /// Create a new instance of the <see cref="EncryptionManager"/> class
        /// </summary>
        /// <param name="encryptionCertificate">
        /// The certificate that contains the secrets used for encryption. Note that
        /// the private key must be present in this certificate in order for decryption
        /// to be performed.
        /// </param>
        /// <param name="mode">The mode (encryption/decryption) of the encryption manager</param>
        /// <param name="outputStream">The stream where the transformed content will be written</param>
        /// <param name="sourceFileSize"></param>
        public EncryptionManager(
            X509Certificate2 encryptionCertificate,
            EncryptionMode mode,
            Stream outputStream,
            long sourceFileSize)
        {
            Pre.ThrowIfArgumentNull(encryptionCertificate, nameof(encryptionCertificate));
            Pre.ThrowIfArgumentNull(outputStream, nameof(outputStream));

            Pre.ThrowIfTrue(mode == EncryptionMode.None, "Encryption mode cannot be None");

            this.encryptionCertificate = encryptionCertificate;
            this.Mode           = mode;
            this.sourceFileSize = sourceFileSize;
            this.outputStream   = outputStream;

            this.sha1 = new SHA1Cng();
            this.md5  = new MD5Cng();

            // Any valid encrypted file will have a minimum size (to include the header and minimal
            // encrypted content). Ensure that the source file is at least this size.
            if (mode == EncryptionMode.Decrypt)
            {
                Pre.Assert(sourceFileSize >= MinimumEncryptedFileSize, "sourceFileSize >= minimumEncryptedFileSize");
            }

            this.Initialize();
        }
Esempio n. 2
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (count <= 0)
            {
                return;
            }

            Pre.ThrowIfArgumentNull(buffer, "buffer");
            Pre.ThrowIfTrue(buffer.Length == 0, "buffer.Length is 0");
            Pre.ThrowIfTrue(offset + count > buffer.Length, "offset + count > buffer.Length");

            switch (this.UploadSession.State)
            {
            case OneDriveFileUploadState.Completed:
                throw new OneDriveException("Cannot write to completed upload session.");

            case OneDriveFileUploadState.Faulted:
                throw new OneDriveException("Cannot write to faulted upload session.");

            case OneDriveFileUploadState.Cancelled:
                throw new OneDriveException("Cannot write to cancelled upload session.");
            }

            // Check if the new data will be more than the file size specified in the session
            if (this.totalSize + count > this.UploadSession.Length)
            {
                Logger.Error(
                    "OneDrive file upload overflow. File={0}, ParentId={1}, Length={2}, CurrentSize={3}, WriteSize={4}",
                    this.UploadSession.ItemName,
                    this.UploadSession.ParentId,
                    this.UploadSession.Length,
                    this.totalSize,
                    count);

                throw new OneDriveException("More data was written to the stream than is allowed by the file.");
            }

            // Allocate a new buffer locally (since the buffer provided by the caller might not exist after the call
            // returns) and copy the given buffer into the local buffer.
            byte[] localBuffer = new byte[count];
            Buffer.BlockCopy(buffer, offset, localBuffer, 0, count);

            // Add the new buffer to the list of buffers and update the total size.
            this.buffers.Add(localBuffer);
            this.totalSize += count;

            // If the total size of the buffers is at least the fragment size, flush the data (sending it to OneDrive).
            if (this.AreFragmentAvailable())
            {
                this.Flush();
            }
        }
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (count <= 0)
            {
                return;
            }

            Pre.ThrowIfArgumentNull(buffer, "buffer");
            Pre.ThrowIfTrue(buffer.Length == 0, "buffer.Length is 0");
            Pre.ThrowIfTrue(offset + count > buffer.Length, "offset + count > buffer.Length");

            base.Write(buffer, offset, count);
        }
Esempio n. 4
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (count <= 0)
            {
                return;
            }

            Pre.ThrowIfArgumentNull(buffer, "buffer");
            Pre.ThrowIfTrue(buffer.Length == 0, "buffer.Length is 0");
            Pre.ThrowIfTrue(offset + count > buffer.Length, "offset + count > buffer.Length");

            if (this.flushInProgress)
            {
                throw new InvalidOperationException("Cannot write while a flush is in progress");
            }

            this.memoryStream.Write(buffer, offset, count);
        }