Beispiel #1
0
        private async void ScanQRCode()
        {
            var options = new MobileBarcodeScanningOptions {
                PossibleFormats = new List <BarcodeFormat> {
                    BarcodeFormat.QR_CODE
                }
            };

            var result = await _barcodeScanner.Scan(options);

            if (result == null)
            {
                return;
            }

            try
            {
                var auth = Authenticator.FromKeyUri(result.Text);

                if (_authSource.IsDuplicate(auth))
                {
                    Toast.MakeText(this, Resource.String.duplicateAuthenticator, ToastLength.Short).Show();
                    return;
                }

                await _connection.InsertAsync(auth);

                await _authSource.UpdateSource();

                CheckEmptyState();
                _authAdapter.NotifyItemInserted(_authSource.GetPosition(auth.Secret));
            }
            catch
            {
                Toast.MakeText(this, Resource.String.qrCodeFormatError, ToastLength.Short).Show();
            }
        }
        private async Task Restore(string password = "")
        {
            try
            {
                string contents;

                if (String.IsNullOrEmpty(password))
                {
                    contents = Encoding.UTF8.GetString(_fileData);
                }
                else
                {
                    var sha256        = SHA256.Create();
                    var passwordBytes = Encoding.UTF8.GetBytes(password);
                    var keyMaterial   = sha256.ComputeHash(passwordBytes);

                    var provider =
                        WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);

                    var key = provider.CreateSymmetricKey(keyMaterial);

                    var raw = WinRTCrypto.CryptographicEngine.Decrypt(key, _fileData);
                    contents = Encoding.UTF8.GetString(raw);
                    _dialog.Dismiss();
                }

                var backup = JsonConvert.DeserializeObject <Backup>(contents);

                if (backup.Authenticators == null)
                {
                    Toast.MakeText(this, Resource.String.invalidFileError, ToastLength.Short).Show();
                    return;
                }

                var authsInserted      = 0;
                var categoriesInserted = 0;

                foreach (var auth in backup.Authenticators.Where(auth => !_authSource.IsDuplicate(auth)))
                {
                    await _connection.InsertAsync(auth);

                    authsInserted++;
                }

                foreach (var category in backup.Categories.Where(category => !_categorySource.IsDuplicate(category)))
                {
                    await _connection.InsertAsync(category);

                    categoriesInserted++;
                }

                foreach (var binding in backup.AuthenticatorCategories.Where(binding => !_authSource.IsDuplicateCategoryBinding(binding)))
                {
                    await _connection.InsertAsync(binding);
                }

                var message = String.Format(GetString(Resource.String.restoredFromBackup), authsInserted,
                                            categoriesInserted);
                Toast.MakeText(this, message, ToastLength.Long).Show();

                Finish();
            }
            catch
            {
                _dialog.Error = GetString(Resource.String.restoreError);
            }
        }