Use a predetermined default password length field

This commit is contained in:
2023-04-16 20:41:33 +02:00
parent 605f345dc0
commit 0987b4c280
3 changed files with 14 additions and 6 deletions

View File

@@ -4,7 +4,8 @@ namespace Common;
public static class PasswordGenerator public static class PasswordGenerator
{ {
public static Error? ExceptionOccured; public static Error? ExceptionOccured;
private static string RandomStr(int length, bool no_symbols = false) public static int DEFAULT_LENGTH = 16;
private static string RandomStr(int length = 16, bool no_symbols = false)
{ {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
Random rnd = new Random(); Random rnd = new Random();

View File

@@ -67,7 +67,7 @@ partial class GeneratePassword
// //
passwordLength.Location = new Point(12, 77); passwordLength.Location = new Point(12, 77);
passwordLength.Name = "passwordLength"; passwordLength.Name = "passwordLength";
passwordLength.PlaceholderText = "16"; passwordLength.PlaceholderText = PasswordGenerator.DEFAULT_LENGTH;
passwordLength.Size = new Size(156, 23); passwordLength.Size = new Size(156, 23);
passwordLength.TabIndex = 3; passwordLength.TabIndex = 3;
// //
@@ -132,4 +132,4 @@ partial class GeneratePassword
private CheckBox noSymbols; private CheckBox noSymbols;
private Button generate; private Button generate;
private Button cancel; private Button cancel;
} }

View File

@@ -19,21 +19,28 @@ public partial class GeneratePassword : Form
public void Generate(object sender, EventArgs e) public void Generate(object sender, EventArgs e)
{ {
if (passwordName.Text == "" || passwordLength.Text == "") if (passwordName.Text == "")
{ {
MessageBox.Show("You must fill in all fields to continue.", "Error: Empty fields", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("You must provide a name to continue.", "Error: Empty field", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
else else
{ {
try try
{ {
int length;
if (!int.TryParse(passwordLength.Text, out length))
{
length = PasswordGenerator.DEFAULT_LENGTH
}
PasswordGenerator.ExceptionOccured += (e) => MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); PasswordGenerator.ExceptionOccured += (e) => MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
string filePath = $"{currentPath}\\{passwordName.Text}"; string filePath = $"{currentPath}\\{passwordName.Text}";
File.WriteAllText( File.WriteAllText(
currentPath + $"\\{passwordName.Text}.gpg", currentPath + $"\\{passwordName.Text}.gpg",
PasswordGenerator.New( PasswordGenerator.New(
recipient, recipient,
Convert.ToInt32(passwordLength.Text), length,
noSymbols.Checked) noSymbols.Checked)
); );
} }