Update CreateVaultDialog.axaml.cs

This commit is contained in:
2026-05-08 00:24:36 +03:00
parent f1fb897266
commit 33f6f86c0b

View File

@@ -4,6 +4,7 @@ using Avalonia.Input;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.Platform.Storage; using Avalonia.Platform.Storage;
using System;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace KeyKeeper.Views namespace KeyKeeper.Views
@@ -14,10 +15,6 @@ namespace KeyKeeper.Views
public string Password { get; private set; } = string.Empty; public string Password { get; private set; } = string.Empty;
public bool Success { get; private set; } public bool Success { get; private set; }
// Добавлено для индикатора сложности пароля
private Border? _strengthBorder;
private TextBlock? _strengthText;
public CreateVaultDialog() public CreateVaultDialog()
{ {
InitializeComponent(); InitializeComponent();
@@ -60,61 +57,67 @@ namespace KeyKeeper.Views
UpdateCreateButtonState(); UpdateCreateButtonState();
PasswordErrorText.IsVisible = false; PasswordErrorText.IsVisible = false;
// Добавлено: обновляем индикатор сложности пароля // Обновляем индикатор сложности пароля
UpdatePasswordStrength(PasswordBox.Text ?? ""); string password = PasswordBox.Text ?? "";
UpdatePasswordStrengthIndicator(password);
} }
// Добавлен метод для оценки сложности пароля private void UpdatePasswordStrengthIndicator(string password)
private void UpdatePasswordStrength(string password)
{ {
if (_strengthBorder == null || _strengthText == null) if (PasswordStrengthFill == null || PasswordStrengthIndicator == null)
{ return;
_strengthBorder = this.FindControl<Border>("StrengthBorder");
_strengthText = this.FindControl<TextBlock>("StrengthText");
if (_strengthBorder == null || _strengthText == null) return;
}
int score = 0;
if (password.Length >= 8) score++;
if (password.Length >= 12) score++;
if (Regex.IsMatch(password, @"\d")) score++;
if (Regex.IsMatch(password, @"[a-z]")) score++;
if (Regex.IsMatch(password, @"[A-Z]")) score++;
if (Regex.IsMatch(password, @"[!@#$%^&*(),.?"":{}|<>]")) score++;
IBrush color;
string text;
double widthPercent;
if (string.IsNullOrEmpty(password)) if (string.IsNullOrEmpty(password))
{ {
color = Brushes.Gray; PasswordStrengthFill.Width = 0;
text = ""; return;
widthPercent = 0;
} }
else if (score < 3)
int strength = CalculatePasswordStrength(password);
double maxWidth = PasswordStrengthIndicator.Bounds.Width;
if (maxWidth <= 0) maxWidth = 200;
PasswordStrengthFill.Width = (strength / 100.0) * maxWidth;
if (strength < 20)
{ {
color = Brushes.Red; PasswordStrengthFill.Background = new SolidColorBrush(Colors.Red);
text = "Weak";
widthPercent = 33;
} }
else if (score < 5) else if (strength < 50)
{ {
color = Brushes.Orange; PasswordStrengthFill.Background = new SolidColorBrush(Colors.Orange);
text = "Medium"; }
widthPercent = 66; else if (strength < 70)
{
PasswordStrengthFill.Background = new SolidColorBrush(Colors.Gold);
} }
else else
{ {
color = Brushes.LimeGreen; PasswordStrengthFill.Background = new SolidColorBrush(Colors.Green);
text = "Strong";
widthPercent = 100;
} }
}
_strengthBorder.Background = color; private int CalculatePasswordStrength(string password)
_strengthBorder.Width = 250 * (widthPercent / 100); {
_strengthText.Text = text; int score = 0;
_strengthText.Foreground = color;
if (password.Length >= 8) score += 20;
if (password.Length >= 12) score += 20;
if (password.Length >= 16) score += 15;
if (Regex.IsMatch(password, @"\d")) score += 10;
if (Regex.IsMatch(password, @"[a-z]")) score += 15;
if (Regex.IsMatch(password, @"[A-Z]")) score += 15;
if (Regex.IsMatch(password, @"[!@#$%^&*()_+\-=\[\]{};':""\\|,.<>\/?]")) score += 20;
var uniqueChars = new System.Collections.Generic.HashSet<char>(password).Count;
score += Math.Min(20, uniqueChars * 2);
return Math.Min(100, score);
} }
private void UpdateCreateButtonState() private void UpdateCreateButtonState()