mirror of
https://github.com/KeyKeeperApp/KeyKeeper.git
synced 2026-05-19 14:56:34 +03:00
added logic of password security
This commit is contained in:
@@ -1,33 +1,100 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Interactivity;
|
using Avalonia.Interactivity;
|
||||||
|
using Avalonia.Media;
|
||||||
using KeyKeeper.PasswordStore;
|
using KeyKeeper.PasswordStore;
|
||||||
using static KeyKeeper.PasswordStore.FileFormatConstants;
|
using static KeyKeeper.PasswordStore.FileFormatConstants;
|
||||||
|
|
||||||
namespace KeyKeeper.Views;
|
namespace KeyKeeper.Views;
|
||||||
|
|
||||||
public partial class EntryEditWindow: Window
|
public partial class EntryEditWindow : Window
|
||||||
{
|
{
|
||||||
public PassStoreEntryPassword? EditedEntry;
|
public PassStoreEntryPassword? EditedEntry;
|
||||||
|
|
||||||
public EntryEditWindow()
|
public EntryEditWindow()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
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)
|
private void AddButton_Click(object sender, RoutedEventArgs args)
|
||||||
{
|
{
|
||||||
string name = EntryNameEdit.Text ?? "";
|
string name = EntryNameEdit?.Text?.Trim() ?? "";
|
||||||
name = name.Trim();
|
if (string.IsNullOrEmpty(name)) return;
|
||||||
if (name.Length == 0) return;
|
|
||||||
|
|
||||||
string username = UsernameEdit.Text ?? "";
|
string username = UsernameEdit?.Text?.Trim() ?? "";
|
||||||
username = username.Trim();
|
if (string.IsNullOrEmpty(username)) return;
|
||||||
if (username.Length == 0) return;
|
|
||||||
|
|
||||||
string password = PasswordEdit.Text ?? "";
|
string password = PasswordEdit?.Text ?? "";
|
||||||
password = password.Trim();
|
if (string.IsNullOrEmpty(password)) return;
|
||||||
if (password.Length == 0) return;
|
|
||||||
|
|
||||||
EditedEntry = new PassStoreEntryPassword(
|
EditedEntry = new PassStoreEntryPassword(
|
||||||
Guid.NewGuid(),
|
Guid.NewGuid(),
|
||||||
|
|||||||
Reference in New Issue
Block a user