Example #1
0
        public static DecryptedStream Create(Stream encryptedStream, string sharedSecret, string salt, int contentLength)
        {
            var instance = new DecryptedStream();

            TaskHelper.WaitSafeSync(() => instance.InitAsync(encryptedStream, sharedSecret, salt, contentLength));

            return(instance);
        }
Example #2
0
        public async static Task <DecryptedStream> CreateAsync(Stream encryptedStream, string sharedSecret, string salt, int contentLength)
        {
            var instance = new DecryptedStream();
            await instance.InitAsync(encryptedStream, sharedSecret, salt, contentLength)
            .IgnoreContext();

            return(instance);
        }
Example #3
0
        public async Task <FileStorageResponse> GetContentAsync(string fileName, StorageEncryptionKeys encryptionKeys)
        {
            Stream contentStream = null;
            Stream returnStream  = null;

            try
            {
                var response = await _storage.GetContentAsync(fileName)
                               .IgnoreContext();

                contentStream = response.Stream;
                int contentLength = (int)response.ContentLength;

                if (encryptionKeys != null)
                {
                    response.Stream = await DecryptedStream.CreateAsync(contentStream, encryptionKeys.Secret, encryptionKeys.Salt, contentLength).IgnoreContext();
                }
                else
                {
                    returnStream = contentStream as MemoryStream;

                    if (returnStream == null)
                    {
                        returnStream = new MemoryStream(contentLength);
                        await contentStream.CopyToAsync(returnStream, Math.Min(80 * 1024, contentLength)).IgnoreContext();
                    }

                    returnStream.Position = 0;
                    response.Stream       = returnStream;
                }

                return(response);
            }
            finally
            {
                //closes the remote stream
                if (contentStream != null && !object.ReferenceEquals(contentStream, returnStream))
                {
                    contentStream.Dispose();
                }
            }
        }