/// <summary> /// Writes the current accounts into the datafile /// </summary> public async void WriteLocalDatafile() { var dbHash = await App.Repository.Password.GetAsync(); var datafileDB = await App.Repository.Datafile.GetAsync(); var iv = new AesManaged().IV; await CollectionAccessSemaphore.WaitAsync(); DatafileModel file = new DatafileModel() { IV = iv, Collection = Collection }; var folder = await StorageFolder.GetFolderFromPathAsync(datafileDB.Path); await FileService.WriteStringAsync( datafileDB.Name, NewtonsoftJSONService.SerializeEncrypt(SecretService.Helper.ReadSecret(Constants.ContainerName, dbHash.Hash), iv, file), folder); CollectionAccessSemaphore.Release(); }
/// <summary> /// Checks and reads the current local datafile /// </summary> /// <param name="dbDatafile"></param> private async void CheckLocalDatafile(DBDatafileModel dbDatafile) { try { ObservableCollection <TwoFACodeModel> deserializeCollection = new ObservableCollection <TwoFACodeModel>(); StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(dbDatafile.Path); if (await FileService.FileExistsAsync(dbDatafile.Name, folder)) { var dbHash = await App.Repository.Password.GetAsync(); // prevent write of the datafile _initialization = true; try { string datafileStr = await FileService.ReadStringAsync(dbDatafile.Name, folder); if (!string.IsNullOrEmpty(datafileStr)) { // read the iv for AES DatafileModel datafile = NewtonsoftJSONService.Deserialize <DatafileModel>(datafileStr); var iv = datafile.IV; datafile = NewtonsoftJSONService.DeserializeDecrypt <DatafileModel>( SecretService.Helper.ReadSecret(Constants.ContainerName, dbHash.Hash), iv, datafileStr); deserializeCollection = datafile.Collection; } } catch (Exception) { ShowPasswordError(); } } // file not found case else { ShowFileOrFolderNotFoundError(); } if (deserializeCollection != null) { Collection.AddRange(deserializeCollection); if (Collection.Count == 0) { // if no error has occured if (!_errorOccurred) { EmptyAccountCollectionTipIsOpen = true; } } else { if (EmptyAccountCollectionTipIsOpen) { EmptyAccountCollectionTipIsOpen = false; } } } } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception exc) #pragma warning restore CA1031 // Do not catch general exception types { if (exc is UnauthorizedAccessException) { ShowUnauthorizedAccessError(); } else if (exc is FileNotFoundException) { ShowFileOrFolderNotFoundError(); } else { _errorOccurred = true; TrackingManager.TrackException(exc); ErrorDialogs.ShowUnexpectedError(exc); } } // writing the data file is activated again _initialization = false; }