예제 #1
0
        /*----------------------------------------------------------------------------------------------------
        * Return a copy of <TModel> with encrypted/decrypted string properties.
        *  ----------------------------------------------------------------------------------------------------*/
        private TModel ApplyCryptography(TModel model, CryptographyMode mode)
        {
            dynamic cryptedModel = Activator.CreateInstance(typeof(TModel));

            foreach (var prop in model.GetType().GetProperties())
            {
                if (prop.PropertyType == typeof(string))
                {
                    string propertyValue = prop.GetValue(model).ToString();
                    if (mode == CryptographyMode.Decrypt)
                    {
                        prop.SetValue(cryptedModel, cryptographer.Decrypt(propertyValue));
                    }
                    else if (mode == CryptographyMode.Encrypt)
                    {
                        prop.SetValue(cryptedModel, cryptographer.Encypt(propertyValue));
                    }
                }
            }
            return(cryptedModel);
        }
예제 #2
0
        private async void TryWritePassword()
        {
            IsUiAvailable = false;
            if (Password == ConfirmPassword)
            {
                if (Password.Length >= minLength && Password.Length <= maxLenght)
                {
                    if (Regex.IsMatch(Password, @"^\d+$")) // Is digits only
                    {
                        SecureManager.Key = Password;
                        string value = Password;
                        if (cryptographer != null)
                        {
                            cryptographer.ChangeKey(SecureManager.Key);
                            value = cryptographer.Encypt(value);
                        }
                        setting.Write(SecureManager.PasswordKey, value);

                        LoadingPanelVisibility = Visibility.Visible;
                        OnPropertyChanged(nameof(LoadingPanelVisibility));
                        Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;

                        if (await Initialize())
                        {
                            Views.MainView mainView = new Views.MainView();
                            foreach (Window item in Application.Current.Windows)
                            {
                                if (item.DataContext == this)
                                {
                                    item.Close();
                                }
                            }

                            Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
                            mainView.ShowDialog();
                        }
                        else
                        {
                            Mouse.OverrideCursor   = System.Windows.Input.Cursors.Arrow;
                            LoadingPanelVisibility = Visibility.Hidden;
                            OnPropertyChanged(nameof(LoadingPanelVisibility));
                            setting.Delete(SecureManager.PasswordKey);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Password should consist only of digits.");
                    }
                }
                else
                {
                    MessageBox.Show($"The number of password characters must be between {minLength} and {maxLenght}.");
                }
            }
            else
            {
                MessageBox.Show("Entered passwords are different.");
            }

            IsUiAvailable = true;
        }
        /*----------------------------------------------------------------------------------------------------
        * Create and return List of <TModel>. Properties of <TModel> will be equal to the following values:
        *
        *       - <DataTime> type : All properties will be generate in <dataGenerator.GenerateRandomDate>
        *                           method.
        *
        *       - <String> type :   Properties with "Login" name will be generate in
        *                           <dataGenerator.GenerateRandomLogin()> method.
        *
        *                           Properties with "Password" name will be generate in
        *                           <dataGenerator.GenerateRandomPassword()> method.
        *
        *                           All other properties will be generate in
        *                           <dataGenerator.GenerateRandomDescription()> method.
        *  ----------------------------------------------------------------------------------------------------*/
        /// <summary>
        /// Return List of TModel with properties generated by IDataGenerator.
        /// </summary>
        /// <param name="modelsCount">Required TModel count in list.</param>
        /// <returns>Generated TModels collection.</returns>
        public List <TModel> GetData(int modelsCount = 10000)
        {
            List <TModel> dataList = new List <TModel>();

            dataList.Capacity = modelsCount;
            var genericType = GetType().GetGenericArguments();

            for (int i = 0; i < modelsCount; i++)
            {
                dynamic model = Activator.CreateInstance(genericType[0]);
                foreach (var prop in (model as TModel).GetType().GetProperties())
                {
                    PropertyInfo pInfo = prop as PropertyInfo;
                    if (pInfo != null)
                    {
                        if (pInfo.PropertyType == typeof(string))
                        {
                            // Without encryption
                            if (cryptographer == null)
                            {
                                switch (pInfo.Name)
                                {
                                case "Login":
                                {
                                    prop.SetValue(model, dataGenerator.GenerateRandomLogin());
                                    break;
                                }

                                case "Password":
                                {
                                    prop.SetValue(model, dataGenerator.GenerateRandomPassword());
                                    break;
                                }

                                case "Date":
                                {
                                    prop.SetValue(model, dataGenerator.GenerateRandomDate());
                                    break;
                                }

                                default:
                                {
                                    prop.SetValue(model, dataGenerator.GenerateRandomDescription());
                                    break;
                                }
                                }
                            }
                            // With encryption
                            else
                            {
                                switch (pInfo.Name)
                                {
                                case "Login":
                                {
                                    prop.SetValue(model, cryptographer.Encypt(dataGenerator.GenerateRandomLogin()));
                                    break;
                                }

                                case "Password":
                                {
                                    prop.SetValue(model, cryptographer.Encypt(dataGenerator.GenerateRandomPassword()));
                                    break;
                                }

                                case "Date":
                                {
                                    prop.SetValue(model, cryptographer.Encypt(dataGenerator.GenerateRandomDate()));
                                    break;
                                }

                                default:
                                {
                                    prop.SetValue(model, cryptographer.Encypt(dataGenerator.GenerateRandomDescription()));
                                    break;
                                }
                                }
                            }
                        }
                    }
                }
                dataList.Add(model);
            }
            return(dataList);
        }