From f08b2e91c8ccc621233ddb0a27362a5736b65ddb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miskolczi=20Rich=C3=A1rd?= Date: Tue, 28 Mar 2023 08:22:08 +0200 Subject: [PATCH] stuff --- Password Manager/GeneratePassword.cs | 17 +++++------ Password Manager/MainForm.Designer.cs | 1 + Password Manager/MainForm.cs | 7 +++++ Password Manager/PasswordGenerator.cs | 41 +++++++++++++++++++++++++-- Password Manager/PasswordListBox.cs | 8 +++++- 5 files changed, 62 insertions(+), 12 deletions(-) diff --git a/Password Manager/GeneratePassword.cs b/Password Manager/GeneratePassword.cs index bd0ac10..aff3575 100644 --- a/Password Manager/GeneratePassword.cs +++ b/Password Manager/GeneratePassword.cs @@ -15,20 +15,21 @@ namespace Password_Manager public void Generate(object sender, EventArgs e) { - if (passwordName.Text == "" && passwordLength.Text == "") + if (passwordName.Text == "" || passwordLength.Text == "") { MessageBox.Show("You must fill in all fields to continue.", "Error: Empty fields", MessageBoxButtons.OK, MessageBoxIcon.Error); } - - bool valid = false; - while (!valid) + else { try { - StreamWriter sw = new StreamWriter(currentPath + $"{passwordName.Text}.txt"); //TODO: rename to .gpg when encryption is possible - valid = true; - sw.Write(Generator.New(Convert.ToInt32(passwordLength.Text), noSymbols.Checked)); - sw.Close(); + string filePath = $"{currentPath}\\{passwordName.Text}"; + File.WriteAllText( + currentPath + $"\\{passwordName.Text}.gpg", + PasswordGenerator.New( + Convert.ToInt32(passwordLength.Text), + noSymbols.Checked) + ); } catch (FormatException) { diff --git a/Password Manager/MainForm.Designer.cs b/Password Manager/MainForm.Designer.cs index c355e01..494399a 100644 --- a/Password Manager/MainForm.Designer.cs +++ b/Password Manager/MainForm.Designer.cs @@ -84,6 +84,7 @@ Cancel.TabIndex = 8; Cancel.Text = "Cancel"; Cancel.UseVisualStyleBackColor = true; + Cancel.Click += CancelPressed; // // MainForm // diff --git a/Password Manager/MainForm.cs b/Password Manager/MainForm.cs index a682d5e..4ecab77 100644 --- a/Password Manager/MainForm.cs +++ b/Password Manager/MainForm.cs @@ -26,6 +26,11 @@ namespace Password_Manager gp.ShowDialog(); } + private void CancelPressed(object sender, EventArgs e) + { + Close(); + } + private void Decrypt(object sender, EventArgs e) { string fileName = ResultList.Text; @@ -55,6 +60,8 @@ namespace Password_Manager { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } + + Close(); } } } \ No newline at end of file diff --git a/Password Manager/PasswordGenerator.cs b/Password Manager/PasswordGenerator.cs index 4a2394c..a4c44af 100644 --- a/Password Manager/PasswordGenerator.cs +++ b/Password Manager/PasswordGenerator.cs @@ -1,10 +1,11 @@ -using System.Text; +using System.Diagnostics; +using System.Text; namespace Password_Generator { - static class Generator + static class PasswordGenerator { - public static string New(int length, bool no_symbols = false) + private static string RandomStr(int length, bool no_symbols = false) { StringBuilder builder = new StringBuilder(); Random rnd = new Random(); @@ -30,5 +31,39 @@ namespace Password_Generator return builder.ToString(); } + + public static string New(int length, bool no_symbols = false) + { + File.WriteAllText(@".\\", RandomStr(length, no_symbols)); + + try + { + Process proc = new Process() + { + StartInfo = new ProcessStartInfo + { + FileName = "echo", //TODO: pipe the return value of RandomStr to gpg with the --quiet --encrypt --recipient {recipient} flags + //Arguments = $"--quiet --decrypt {filePath}", + UseShellExecute = false, + RedirectStandardOutput = true, + CreateNoWindow = true + } + }; + + proc.Start(); + while (!proc.StandardOutput.EndOfStream) + { + string line = proc.StandardOutput.ReadLine(); + Clipboard.SetText(line); + //Process.Start("./ToastNotification.exe", $"\"{fileName} decrypted\" \"Password copied to clipboard\""); + } + } + catch (Exception ex) + { + MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + return null; + } } } diff --git a/Password Manager/PasswordListBox.cs b/Password Manager/PasswordListBox.cs index 690abbd..22609d7 100644 --- a/Password Manager/PasswordListBox.cs +++ b/Password Manager/PasswordListBox.cs @@ -61,7 +61,13 @@ namespace Password_Manager } this.DataSource = elements; - SelectedIndex = 0; + try + { + SelectedIndex = 0; + } catch (ArgumentOutOfRangeException) + { + SelectedIndex = -1; + } } } }