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.Media;
using Avalonia.Platform.Storage;
using System;
using System.Text.RegularExpressions;
namespace KeyKeeper.Views
@@ -14,10 +15,6 @@ namespace KeyKeeper.Views
public string Password { get; private set; } = string.Empty;
public bool Success { get; private set; }
// Добавлено для индикатора сложности пароля
private Border? _strengthBorder;
private TextBlock? _strengthText;
public CreateVaultDialog()
{
InitializeComponent();
@@ -60,61 +57,67 @@ namespace KeyKeeper.Views
UpdateCreateButtonState();
PasswordErrorText.IsVisible = false;
// Добавлено: обновляем индикатор сложности пароля
UpdatePasswordStrength(PasswordBox.Text ?? "");
// Обновляем индикатор сложности пароля
string password = PasswordBox.Text ?? "";
UpdatePasswordStrengthIndicator(password);
}
// Добавлен метод для оценки сложности пароля
private void UpdatePasswordStrength(string password)
private void UpdatePasswordStrengthIndicator(string password)
{
if (_strengthBorder == null || _strengthText == null)
{
_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 (PasswordStrengthFill == null || PasswordStrengthIndicator == null)
return;
if (string.IsNullOrEmpty(password))
{
color = Brushes.Gray;
text = "";
widthPercent = 0;
PasswordStrengthFill.Width = 0;
return;
}
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;
text = "Weak";
widthPercent = 33;
PasswordStrengthFill.Background = new SolidColorBrush(Colors.Red);
}
else if (score < 5)
else if (strength < 50)
{
color = Brushes.Orange;
text = "Medium";
widthPercent = 66;
PasswordStrengthFill.Background = new SolidColorBrush(Colors.Orange);
}
else if (strength < 70)
{
PasswordStrengthFill.Background = new SolidColorBrush(Colors.Gold);
}
else
{
color = Brushes.LimeGreen;
text = "Strong";
widthPercent = 100;
PasswordStrengthFill.Background = new SolidColorBrush(Colors.Green);
}
}
_strengthBorder.Background = color;
_strengthBorder.Width = 250 * (widthPercent / 100);
_strengthText.Text = text;
_strengthText.Foreground = color;
private int CalculatePasswordStrength(string password)
{
int score = 0;
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()