Exemplo n.º 1
0
        public async Task NukeFiles()
        {
            if(!Initialized)
            {
                throw new ServiceNotInitializedException($"{nameof(FileService)} was not initialized before access was attempted.");
            }

            using (await f_lock.Acquire())
            {
                for (int i = _localFiles.Count - 1; i >= 0; i--)
                {
                    await DeleteFileAsync((StorageFile)_localFiles[i].BackingFile, FileLocation.Local);
                }
                for (int i = _localFiles.Count - 1; i >= 0; i--)
                {
                    await DeleteFileAsync((StorageFile)_roamedFiles[i].BackingFile, FileLocation.Roamed);
                }


            IVStorage ivs = new IVStorage();
            await ivs.LoadFromStorage();
            ivs.FileNameIVDict.Clear();
            await ivs.SaveToStorage();
            }
        }
Exemplo n.º 2
0
        public async Task RenameFileAsync(IBindableStorageFile file, string newName)
        {
            if(!Initialized)
            {
                throw new ServiceNotInitializedException($"{nameof(FileService)} was not initialized before access was attempted.");
            }

            using (await f_lock.Acquire())
            {
                string oldName = file.BackingFile.Name;
                await FileUtilities.RenameFileAsync((StorageFile)file.BackingFile, newName);
                file.NameChanged();

                IVStorage ivs = new IVStorage();
                await ivs.LoadFromStorage();
                string iv = ivs.FileNameIVDict[oldName];
                ivs.FileNameIVDict.Remove(oldName);
                ivs.FileNameIVDict.Add(newName, iv);
                await ivs.SaveToStorage();
            }
        }
Exemplo n.º 3
0
        public async Task<string> RetrieveFileContentsAsync(string fileName, string password, FileLocation location)
        {
            if (!Initialized)
            {
                throw new ServiceNotInitializedException($"{nameof(FileService)} was not initialized before access was attempted.");
            }

            using (await f_lock.Acquire())
            {
                var collectionToSearch = location == FileLocation.Local ? _localFiles : _roamedFiles;
                string encryptedContents = await FileIO.ReadTextAsync(collectionToSearch.Single(x => x.Name == fileName).BackingFile);
                if (String.IsNullOrWhiteSpace(encryptedContents))
                {
                    return null;
                }

                IVStorage ivs = new IVStorage();
                await ivs.LoadFromStorage();
                string iv = ivs.FileNameIVDict[fileName];
                try
                {
                    return EncryptionManager.Decrypt(encryptedContents, password, iv);
                }
                catch(Exception ex)
                {
                    //Just explicitly noting that .Decrypt() can and WILL throw.
                    throw;
                }
            }

        }
Exemplo n.º 4
0
        public async Task DeleteFileAsync(StorageFile backingFile, FileLocation location)
        {
            if (!Initialized)
            {
                throw new ServiceNotInitializedException($"{nameof(FileService)} was not initialized before access was attempted.");
            }

            using (await f_lock.Acquire())
            {
                string fileName = backingFile.Name;
                var collectionToSearch = location == FileLocation.Local ? _localFiles : _roamedFiles;
                collectionToSearch.Remove(collectionToSearch.Single(x => x.BackingFile == backingFile));
                await FileUtilities.DeleteFileAsync(backingFile);

                IVStorage ivs = new IVStorage();
                await ivs.LoadFromStorage();
                ivs.FileNameIVDict.Remove(fileName);
                await ivs.SaveToStorage();
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Saves and encrypts the contents into a StorageFile with the given name, and the given password.
        /// </summary>
        /// <param name="contents">The contents to encrypt.</param>
        /// <param name="fileName">The filename with which to save the file.</param>
        /// <param name="password">The password that will be used to generate the encryption key.</param>
        /// <returns>If successful, returns the created StorageFile.</returns>
        public async Task<BindableStorageFile> SaveAndEncryptFileAsync(string contents, string fileName, string password)
        {
            using (await f_lock.Acquire())
            {
                if (!Initialized)
                {
                    throw new ServiceNotInitializedException($"{nameof(FileService)} was not initialized before access was attempted.");
                }
                if(contents == null)
                {
                    throw new ArgumentException("File contents cannot be null.");
                }

                string iv = Guid.NewGuid().ToString("N");
                
                string encryptedContents = EncryptionManager.Encrypt(contents, password, iv);
                var savedFile = await FileUtilities.GetLocalFolder().CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName);
                await FileIO.WriteTextAsync(savedFile, encryptedContents);
                string savedFileName = savedFile.Name;
                
                BindableStorageFile bsf = await BindableStorageFile.Create(savedFile);                
                _localFiles.Add(bsf);

                IVStorage ivs = new IVStorage();
                await ivs.LoadFromStorage();
                ivs.FileNameIVDict.Add(bsf.BackingFile.Name, iv);
                await ivs.SaveToStorage();

                return bsf;
            }
        }