Example #1
0
        private static string DecryptedFile(IIngestManifestFile ingestManifestFile, string encryptedPath, CloudMediaContext context)
        {
            IIngestManifestAsset ingestManifestAsset = context.IngestManifestAssets.Where(a => a.Id == ingestManifestFile.ParentIngestManifestAssetId).FirstOrDefault();

            Assert.IsNotNull(ingestManifestAsset);

            IList <IContentKey> keys = ingestManifestAsset.Asset.ContentKeys.Where(c => c.ContentKeyType == ContentKeyType.StorageEncryption).ToList();

            Assert.AreEqual(1, keys.Count, "Expecting only one storage key per asset");
            IContentKey key = keys.FirstOrDefault();

            Assert.IsNotNull(ingestManifestAsset);


            Guid           keyId          = EncryptionUtils.GetKeyIdAsGuid(key.Id);
            FileEncryption fileEncryption = new FileEncryption(key.GetClearKeyValue(), keyId);

            ulong iv            = Convert.ToUInt64(ingestManifestFile.InitializationVector, CultureInfo.InvariantCulture);
            var   decryptedPath = @".\Resources\TestFiles\Decrypted" + Guid.NewGuid();

            if (!Directory.Exists(decryptedPath))
            {
                Directory.CreateDirectory(decryptedPath);
            }

            decryptedPath = Path.Combine(decryptedPath, ingestManifestFile.Name);
            FileInfo      fileInfo        = new FileInfo(encryptedPath);
            var           maxblocksize    = GetBlockSize(fileInfo.Length);
            List <string> blockList       = new List <string>();
            int           numberOfthreads = 1;
            var           queue           = PreapreDownloadQueue(maxblocksize, fileInfo.Length, ref numberOfthreads, out blockList);

            using (var fs = new FileStream(decryptedPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
            {
                KeyValuePair <int, int> block;
                while (queue.TryDequeue(out block))
                {
                    fs.Seek(block.Key * maxblocksize, SeekOrigin.Begin);
                    using (FileStream stream = File.OpenRead(encryptedPath))
                    {
                        byte[] buffer = new byte[block.Value];
                        stream.Seek(block.Key * maxblocksize, SeekOrigin.Begin);
                        int read = stream.Read(buffer, 0, (int)block.Value);
                        if (fileEncryption != null)
                        {
                            lock (fileEncryption)
                            {
                                using (FileEncryptionTransform encryptor = fileEncryption.GetTransform(iv, block.Key * maxblocksize))
                                {
                                    encryptor.TransformBlock(inputBuffer: buffer, inputOffset: 0, inputCount: buffer.Length, outputBuffer: buffer, outputOffset: 0);
                                }
                            }
                        }

                        fs.Write(buffer, 0, buffer.Length);
                    }
                }
            }
            return(decryptedPath);
        }
Example #2
0
        public override BlobTransferClient GetBlobTransferClient()
        {
            Mock <BlobTransferClient> mock = new Mock <BlobTransferClient>(default(TimeSpan));

            mock.Setup(c => c.UploadBlob(It.IsAny <Uri>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <FileEncryption>(), It.IsAny <CancellationToken>(), It.IsAny <IRetryPolicy>())).Returns((Uri url,
                                                                                                                                                                                                       string localFile,
                                                                                                                                                                                                       string contentType,
                                                                                                                                                                                                       FileEncryption fileEncryption,
                                                                                                                                                                                                       CancellationToken cancellationToken,
                                                                                                                                                                                                       IRetryPolicy retryPolicy) => Task.Factory.StartNew(() =>
            {
                FileInfo info = new FileInfo(localFile);
                if (fileEncryption != null)
                {
                    lock (fileEncryption)
                    {
                        using (FileEncryptionTransform encryptor = fileEncryption.GetTransform(info.Name, 0))
                        {
                        }
                    }
                }
            }));
            mock.Setup(c => c.DownloadBlob(It.IsAny <Uri>(), It.IsAny <string>(), It.IsAny <FileEncryption>(), It.IsAny <ulong>(), It.IsAny <CancellationToken>(), It.IsAny <IRetryPolicy>())).Returns(() => Task.Factory.StartNew(() => { }));
            mock.Setup(c => c.DownloadBlob(It.IsAny <Uri>(), It.IsAny <string>(), It.IsAny <FileEncryption>(), It.IsAny <ulong>(), It.IsAny <CloudBlobClient>(), It.IsAny <CancellationToken>(), It.IsAny <IRetryPolicy>())).Returns(() => Task.Factory.StartNew(() => { }));
            return(mock.Object);
        }