public static EncryptedStream Create(Stream originalStream, string sharedSecret, string salt)
        {
            var instance = new EncryptedStream();

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

            return(instance);
        }
        public async static Task <EncryptedStream> CreateAsync(Stream originalStream, string sharedSecret, string salt)
        {
            var instance = new EncryptedStream();
            await instance.InitAsync(originalStream, sharedSecret, salt)
            .IgnoreContext();

            return(instance);
        }
Example #3
0
        /// <summary>
        /// Save a file from various sources to the remote storage
        /// </summary>
        /// <param name="command">Options for the saving the file</param>
        /// <returns>name of file as stored in the remote storage</returns>
        public async Task <string> SaveAsync(StorageCommand command)
        {
            var    sourceProvider = command.SourceProvider;
            Stream sourceStream   = null;

            try
            {
                sourceStream = await sourceProvider.GetStreamAsync().IgnoreContext();

                if (String.IsNullOrEmpty(command.FileName))
                {
                    command.FileName = String.Format("{0}{1}", Guid.NewGuid().ToShortGuid(), command.Extension);
                }
                else
                {
                    if (!String.IsNullOrEmpty(command.Extension) && String.IsNullOrEmpty(Path.GetExtension(command.FileName)))
                    {
                        command.FileName = $"{command.FileName}{command.Extension}";
                    }
                }

                if (String.IsNullOrEmpty(command.ContentType))
                {
                    command.ContentType = GlobalConfiguration.Instance.GetMimeMapping().GetMimeType(command.FileName);
                }

                if (!String.IsNullOrEmpty(command.Folder))
                {
                    command.FileName = "{0}/{1}".FormatWith(command.Folder, command.FileName);
                }

                var keys = command.EncryptionOptions?.GetKeys(command.FileName);
                if (keys != null)
                {
                    using (var streamToSave = await EncryptedStream.CreateAsync(sourceStream, keys.Secret, keys.Salt)
                                              .IgnoreContext())
                    {
                        await _storage.SaveAsync(streamToSave, command.FileName, command.ContentType)
                        .IgnoreContext();
                    }
                }
                else
                {
                    await _storage.SaveAsync(sourceStream, command.FileName, command.ContentType)
                    .IgnoreContext();
                }
            }
            finally
            {
                if (sourceStream != null && sourceProvider.AutoDispose)
                {
                    sourceStream.Dispose();
                }
            }

            return(command.FileName);
        }