From 9e1d1752faa51482e7114f0824827173bdf47980 Mon Sep 17 00:00:00 2001 From: Chernykh Aleksandr Date: Sat, 28 Feb 2026 23:00:35 +0300 Subject: [PATCH] added logic of password security --- src/KeyKeeper/Views/EntryEditWindow.axaml.cs | 87 +++++++++++++++++--- 1 file changed, 77 insertions(+), 10 deletions(-) diff --git a/src/KeyKeeper/Views/EntryEditWindow.axaml.cs b/src/KeyKeeper/Views/EntryEditWindow.axaml.cs index 65cb607..bf46683 100644 --- a/src/KeyKeeper/Views/EntryEditWindow.axaml.cs +++ b/src/KeyKeeper/Views/EntryEditWindow.axaml.cs @@ -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(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(),