From 0987b4c28039f2e41aa059d1c6d1ef126b40691c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miskolczi=20Rich=C3=A1rd?= Date: Sun, 16 Apr 2023 20:41:33 +0200 Subject: [PATCH] Use a predetermined default password length field --- Common/PasswordGenerator.cs | 3 ++- GUI/GeneratePassword.Designer.cs | 4 ++-- GUI/GeneratePassword.cs | 13 ++++++++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Common/PasswordGenerator.cs b/Common/PasswordGenerator.cs index f71cee0..84032a0 100644 --- a/Common/PasswordGenerator.cs +++ b/Common/PasswordGenerator.cs @@ -4,7 +4,8 @@ namespace Common; public static class PasswordGenerator { 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(); Random rnd = new Random(); diff --git a/GUI/GeneratePassword.Designer.cs b/GUI/GeneratePassword.Designer.cs index 55fa90f..1688916 100644 --- a/GUI/GeneratePassword.Designer.cs +++ b/GUI/GeneratePassword.Designer.cs @@ -67,7 +67,7 @@ partial class GeneratePassword // passwordLength.Location = new Point(12, 77); passwordLength.Name = "passwordLength"; - passwordLength.PlaceholderText = "16"; + passwordLength.PlaceholderText = PasswordGenerator.DEFAULT_LENGTH; passwordLength.Size = new Size(156, 23); passwordLength.TabIndex = 3; // @@ -132,4 +132,4 @@ partial class GeneratePassword private CheckBox noSymbols; private Button generate; private Button cancel; -} \ No newline at end of file +} diff --git a/GUI/GeneratePassword.cs b/GUI/GeneratePassword.cs index 53a0146..2fd3819 100644 --- a/GUI/GeneratePassword.cs +++ b/GUI/GeneratePassword.cs @@ -19,21 +19,28 @@ public partial class GeneratePassword : Form 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 { + 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); string filePath = $"{currentPath}\\{passwordName.Text}"; File.WriteAllText( currentPath + $"\\{passwordName.Text}.gpg", PasswordGenerator.New( recipient, - Convert.ToInt32(passwordLength.Text), + length, noSymbols.Checked) ); }