-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyPasswordGenerator.cs
More file actions
385 lines (335 loc) · 15.6 KB
/
MyPasswordGenerator.cs
File metadata and controls
385 lines (335 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// MyPasswordGenerator
// Author: Claude Peters (https://github.com/Morgoth01)
// Copyright (c) 2025 Claude Peters
// License: GPL-3.0
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using Zxcvbn;
namespace MyPasswordGenerator
{
public partial class MainWindow : Window
{
private readonly char[] Lowercase = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
private readonly char[] Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
private readonly char[] Digits = "0123456789".ToCharArray();
private readonly char[] Symbols = "!@#$%^&*()_-+=[]{}|;:,.<>?".ToCharArray();
private string[] EnglishWords;
private string[] GermanWords;
private DispatcherTimer _copyTimer = new DispatcherTimer();
private DispatcherTimer _passphraseTimer = new DispatcherTimer();
private bool _isDarkMode = false;
public MainWindow()
{
InitializeComponent();
var theme = LoadThemeSetting();
if (theme == "Dark")
SetDarkTheme_Click(null, null);
else
SetLightTheme_Click(null, null);
InitializeTimers();
LoadWordLists();
}
private void InitializeTimers()
{
_copyTimer.Interval = TimeSpan.FromSeconds(3);
_copyTimer.Tick += (s, e) =>
{
txtCopiedMessage.Visibility = Visibility.Collapsed;
_copyTimer.Stop();
};
_passphraseTimer.Interval = TimeSpan.FromSeconds(3);
_passphraseTimer.Tick += (s, e) =>
{
txtCopiedMessagePassphrase.Visibility = Visibility.Collapsed;
_passphraseTimer.Stop();
};
}
private void LoadWordLists()
{
try
{
string basePath = AppDomain.CurrentDomain.BaseDirectory;
EnglishWords = File.ReadAllLines(Path.Combine(basePath, "Resources", "english.txt"));
GermanWords = File.ReadAllLines(Path.Combine(basePath, "Resources", "german.txt"));
}
catch (Exception ex)
{
MessageBox.Show($"Error loading word lists: {ex.Message}");
}
}
private string ThemeConfigPath => Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"MyPasswordGenerator_theme.txt");
private void SaveThemeSetting(string theme)
{
try { File.WriteAllText(ThemeConfigPath, theme); } catch { }
}
private string LoadThemeSetting()
{
try { return File.Exists(ThemeConfigPath) ? File.ReadAllText(ThemeConfigPath) : null; } catch { return null; }
}
private string GenerateSecurePassword(char[] charset, int length)
{
var buffer = new byte[length];
RandomNumberGenerator.Fill(buffer);
var result = new StringBuilder(length);
foreach (byte b in buffer)
{
result.Append(charset[b % charset.Length]);
}
return result.ToString();
}
private string GenerateSecurePassphrase(string[] wordList, int wordCount, string separator)
{
var result = new StringBuilder();
for (int i = 0; i < wordCount; i++)
{
var indexBuffer = new byte[4];
RandomNumberGenerator.Fill(indexBuffer);
int index = Math.Abs(BitConverter.ToInt32(indexBuffer, 0)) % wordList.Length;
result.Append(wordList[index] + separator);
}
return result.ToString().TrimEnd(separator.ToCharArray());
}
private Color GetStrengthColor(double entropy, bool isDarkMode)
{
if (isDarkMode)
{
if (entropy < 28) return Color.FromRgb(255, 120, 120); // Soft Red
if (entropy < 36) return Color.FromRgb(255, 200, 120); // Soft Orange
if (entropy < 60) return Color.FromRgb(255, 240, 180); // Soft Yellow
if (entropy < 80) return Color.FromRgb(130, 255, 170); // Mint
if (entropy < 100) return Color.FromRgb(120, 220, 200); // Soft Green
return Color.FromRgb(180, 240, 255); // Soft Cyan
}
else
{
if (entropy < 28) return Color.FromRgb(180, 0, 0); // Dark Red
if (entropy < 36) return Color.FromRgb(200, 120, 0); // Dark Orange
if (entropy < 60) return Color.FromRgb(160, 140, 0); // Olive
if (entropy < 80) return Color.FromRgb(0, 120, 60); // Dark Green
if (entropy < 100) return Color.FromRgb(0, 80, 120); // Teal
return Color.FromRgb(0, 60, 160); // Blue
}
}
private Color GetCheckTabStrengthColor(double entropy, bool isDarkMode)
{
if (isDarkMode)
{
if (entropy < 28) return Color.FromRgb(255, 120, 120); // Soft Red
if (entropy < 36) return Color.FromRgb(255, 200, 120); // Soft Orange
if (entropy < 60) return Color.FromRgb(255, 240, 180); // Soft Yellow
if (entropy < 80) return Color.FromRgb(130, 255, 170); // Mint
if (entropy < 100) return Color.FromRgb(120, 220, 200); // Soft Green
return Color.FromRgb(180, 240, 255); // Soft Cyan
}
else
{
if (entropy < 28) return Color.FromRgb(180, 0, 0); // Dark Red
if (entropy < 36) return Color.FromRgb(200, 120, 0); // Dark Orange
if (entropy < 60) return Color.FromRgb(160, 140, 0); // Olive
if (entropy < 80) return Color.FromRgb(0, 120, 60); // Dark Green
if (entropy < 100) return Color.FromRgb(0, 80, 120); // Teal
return Color.FromRgb(0, 60, 160); // Blue
}
}
private void UpdateStrengthDisplay(double entropy, ProgressBar bar, TextBlock textBlock)
{
bar.Maximum = 128;
AnimateProgressBar(bar, entropy);
string strength;
if (entropy < 28)
strength = "Very Weak";
else if (entropy < 36)
strength = "Weak";
else if (entropy < 60)
strength = "Moderate";
else if (entropy < 80)
strength = "Strong";
else
strength = "Very Strong";
textBlock.Text = $"{entropy:F1} bits ({strength})";
textBlock.Foreground = new SolidColorBrush(GetStrengthColor(entropy, _isDarkMode));
}
private string GetCheckTabStrengthText(double entropy)
{
if (entropy < 28)
return "Congrats! Your password is so weak, a toddler mashing a keyboard could break in";
else if (entropy < 36)
return "Weak";
else if (entropy < 60)
return "Moderate";
else if (entropy < 80)
return "Strong";
else if (entropy < 150)
return "Very Strong";
else
return "YOU SHALL NOT PASS";
}
private void AnimateProgressBar(ProgressBar bar, double toValue)
{
DoubleAnimation anim = new DoubleAnimation
{
To = Math.Min(toValue, bar.Maximum),
Duration = TimeSpan.FromMilliseconds(350),
EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseOut }
};
bar.BeginAnimation(ProgressBar.ValueProperty, anim);
}
private void GeneratePassword_Click(object sender, RoutedEventArgs e)
{
var charset = new List<char>();
if (cbLower.IsChecked == true) charset.AddRange(Lowercase);
if (cbUpper.IsChecked == true) charset.AddRange(Uppercase);
if (cbDigits.IsChecked == true) charset.AddRange(Digits);
if (cbSymbols.IsChecked == true) charset.AddRange(Symbols);
if (charset.Count == 0)
{
MessageBox.Show("Please select at least one character type");
return;
}
string password = GenerateSecurePassword(charset.ToArray(), (int)sliderLength.Value);
txtPassword.Text = password;
double entropy = password.Length * Math.Log(charset.Count, 2);
UpdateStrengthDisplay(entropy, pbStrength, tbStrengthText);
var zxcvbnResult = Core.EvaluatePassword(password);
UpdateStrengthDisplay(entropy, pbStrength, tbStrengthText);
txtPasswordDetails.Text = BuildAnalysisReport(zxcvbnResult, false);
}
private void GeneratePassphrase_Click(object sender, RoutedEventArgs e)
{
var selectedWords = new List<string>();
if (cbEnglish.IsChecked == true) selectedWords.AddRange(EnglishWords);
if (cbGerman.IsChecked == true) selectedWords.AddRange(GermanWords);
if (selectedWords.Count == 0)
{
MessageBox.Show("Please select at least one language");
return;
}
string passphrase = GenerateSecurePassphrase(
selectedWords.ToArray(),
(int)sliderWords.Value,
txtSeparator.Text
);
txtPassphrase.Text = passphrase;
var zxcvbnResult = Core.EvaluatePassword(passphrase);
txtPassphraseDetails.Text = BuildAnalysisReport(zxcvbnResult, false);
}
private void CheckUserPassword_Click(object sender, RoutedEventArgs e)
{
string pw = userCheckPasswordBox.Password;
if (string.IsNullOrWhiteSpace(pw))
{
MessageBox.Show("Please enter a password.");
return;
}
double entropy = pw.Length > 0 ? pw.Length * Math.Log(94, 2) : 0;
AnimateProgressBar(pbUserCheckStrength, entropy);
var result = Core.EvaluatePassword(pw);
// Wenn eine Warning existiert, NUR die Bits und die Warning anzeigen
if (!string.IsNullOrEmpty(result.Feedback.Warning))
{
tbUserCheckStrengthText.Text = $"{entropy:F1} bits\n⚠️ {result.Feedback.Warning}";
}
else
{
tbUserCheckStrengthText.Text = $"{entropy:F1} bits — {GetCheckTabStrengthText(entropy)}";
}
tbUserCheckStrengthText.Foreground = new SolidColorBrush(GetCheckTabStrengthColor(entropy, _isDarkMode));
txtUserCheckDetails.Text = BuildAnalysisReport(result, false); // Passwort nicht anzeigen
}
private string BuildAnalysisReport(Result result, bool showPassword = true)
{
var sb = new StringBuilder();
if (showPassword)
sb.AppendLine($"Password: {result.Password}");
sb.AppendLine($"Strength Score: {result.Score}/4");
sb.AppendLine($"Guesses (log10): {result.GuessesLog10:F1}");
sb.AppendLine("\n[ Crack Time Estimates ]");
sb.AppendLine($"Online (100 guesses/hour): {result.CrackTimeDisplay.OnlineThrottling100PerHour}");
sb.AppendLine($"Offline (slow hash): {result.CrackTimeDisplay.OfflineSlowHashing1e4PerSecond}");
sb.AppendLine($"Offline (fast hash): {result.CrackTimeDisplay.OfflineFastHashing1e10PerSecond}");
if (!string.IsNullOrEmpty(result.Feedback.Warning))
sb.AppendLine($"\nWarning: {result.Feedback.Warning}");
return sb.ToString();
}
private void SetDarkTheme_Click(object sender, RoutedEventArgs e)
{
_isDarkMode = true;
Resources["WindowBackgroundBrush"] = Resources["DarkWindowBackgroundBrush"];
Resources["CardBrush"] = Resources["DarkCardBrush"];
Resources["TextBrush"] = Resources["DarkTextBrush"];
Resources["AccentBrush"] = Resources["DarkAccentBrush"];
Resources["ButtonBrush"] = Resources["DarkButtonBrush"];
Resources["ButtonHoverBrush"] = Resources["DarkButtonHoverBrush"];
Resources["ButtonTextBrush"] = Resources["DarkButtonTextBrush"];
Resources["DynamicBorderColor"] = Resources["DarkBorderBrush"];
SaveThemeSetting("Dark");
}
private void SetLightTheme_Click(object sender, RoutedEventArgs e)
{
_isDarkMode = false;
Resources["WindowBackgroundBrush"] = Resources["LightWindowBackgroundBrush"];
Resources["CardBrush"] = Resources["LightCardBrush"];
Resources["TextBrush"] = Resources["LightTextBrush"];
Resources["AccentBrush"] = Resources["LightAccentBrush"];
Resources["ButtonBrush"] = Resources["LightButtonBrush"];
Resources["ButtonHoverBrush"] = Resources["LightButtonHoverBrush"];
Resources["ButtonTextBrush"] = Resources["LightButtonTextBrush"];
Resources["DynamicBorderColor"] = Resources["LightBorderBrush"];
SaveThemeSetting("Light");
}
private void CopyPassword_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(txtPassword.Text))
{
Clipboard.SetText(txtPassword.Text);
txtCopiedMessage.Visibility = Visibility.Visible;
_copyTimer.Start();
}
}
private void CopyPassphrase_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(txtPassphrase.Text))
{
Clipboard.SetText(txtPassphrase.Text);
txtCopiedMessagePassphrase.Visibility = Visibility.Visible;
_passphraseTimer.Start();
}
}
private void OpenGitHub_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = "https://github.com/Morgoth01/MyPasswordGenerator",
UseShellExecute = true
});
}
private void OpenLatestRelease_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = "https://github.com/Morgoth01/MyPasswordGenerator/releases/latest",
UseShellExecute = true
});
}
// Damit Hyperlinks im TextBlock funktionieren:
private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = e.Uri.AbsoluteUri,
UseShellExecute = true
});
e.Handled = true;
}
}
}