added logic of password security

This commit is contained in:
Chernykh Aleksandr
2026-02-28 23:00:35 +03:00
parent b582a213a7
commit 9e1d1752fa

View File

@@ -1,33 +1,100 @@
using System;
using System.Text.RegularExpressions;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Media;
using KeyKeeper.PasswordStore;
using static KeyKeeper.PasswordStore.FileFormatConstants;
namespace KeyKeeper.Views;
public partial class EntryEditWindow: Window
public partial class EntryEditWindow : Window
{
public PassStoreEntryPassword? EditedEntry;
public EntryEditWindow()
{
InitializeComponent();
if (PasswordEdit != null)
{
PasswordEdit.TextChanged += PasswordTextChanged;
}
}
private void PasswordTextChanged(object? sender, TextChangedEventArgs e)
{
string password = PasswordEdit?.Text ?? "";
UpdatePasswordStrengthIndicator(password);
}
private void UpdatePasswordStrengthIndicator(string password)
{
if (PasswordStrengthFill == null || PasswordStrengthIndicator == null)
return;
if (string.IsNullOrEmpty(password))
{
PasswordStrengthFill.Width = 0;
return;
}
int strength = CalculatePasswordStrength(password);
double maxWidth = PasswordStrengthIndicator.Bounds.Width;
if (maxWidth <= 0) maxWidth = 200;
PasswordStrengthFill.Width = (strength / 100.0) * maxWidth;
if (strength < 30)
{
PasswordStrengthFill.Background = new SolidColorBrush(Colors.Red);
}
else if (strength < 60)
{
PasswordStrengthFill.Background = new SolidColorBrush(Colors.Orange);
}
else if (strength < 80)
{
PasswordStrengthFill.Background = new SolidColorBrush(Colors.Gold);
}
else
{
PasswordStrengthFill.Background = new SolidColorBrush(Colors.Green);
}
}
private int CalculatePasswordStrength(string password)
{
int score = 0;
if (password.Length >= 8) score += 10;
if (password.Length >= 12) score += 10;
if (password.Length >= 16) score += 5;
if (Regex.IsMatch(password, @"\d")) score += 10;
if (Regex.IsMatch(password, @"[a-z]")) score += 10;
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 AddButton_Click(object sender, RoutedEventArgs args)
{
string name = EntryNameEdit.Text ?? "";
name = name.Trim();
if (name.Length == 0) return;
string name = EntryNameEdit?.Text?.Trim() ?? "";
if (string.IsNullOrEmpty(name)) return;
string username = UsernameEdit.Text ?? "";
username = username.Trim();
if (username.Length == 0) return;
string username = UsernameEdit?.Text?.Trim() ?? "";
if (string.IsNullOrEmpty(username)) return;
string password = PasswordEdit.Text ?? "";
password = password.Trim();
if (password.Length == 0) return;
string password = PasswordEdit?.Text ?? "";
if (string.IsNullOrEmpty(password)) return;
EditedEntry = new PassStoreEntryPassword(
Guid.NewGuid(),