This commit is contained in:
2023-03-28 08:22:08 +02:00
parent 35db2e56d4
commit f08b2e91c8
5 changed files with 62 additions and 12 deletions

View File

@@ -15,20 +15,21 @@ namespace Password_Manager
public void Generate(object sender, EventArgs e) 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); MessageBox.Show("You must fill in all fields to continue.", "Error: Empty fields", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
else
bool valid = false;
while (!valid)
{ {
try try
{ {
StreamWriter sw = new StreamWriter(currentPath + $"{passwordName.Text}.txt"); //TODO: rename to .gpg when encryption is possible string filePath = $"{currentPath}\\{passwordName.Text}";
valid = true; File.WriteAllText(
sw.Write(Generator.New(Convert.ToInt32(passwordLength.Text), noSymbols.Checked)); currentPath + $"\\{passwordName.Text}.gpg",
sw.Close(); PasswordGenerator.New(
Convert.ToInt32(passwordLength.Text),
noSymbols.Checked)
);
} }
catch (FormatException) catch (FormatException)
{ {

View File

@@ -84,6 +84,7 @@
Cancel.TabIndex = 8; Cancel.TabIndex = 8;
Cancel.Text = "Cancel"; Cancel.Text = "Cancel";
Cancel.UseVisualStyleBackColor = true; Cancel.UseVisualStyleBackColor = true;
Cancel.Click += CancelPressed;
// //
// MainForm // MainForm
// //

View File

@@ -26,6 +26,11 @@ namespace Password_Manager
gp.ShowDialog(); gp.ShowDialog();
} }
private void CancelPressed(object sender, EventArgs e)
{
Close();
}
private void Decrypt(object sender, EventArgs e) private void Decrypt(object sender, EventArgs e)
{ {
string fileName = ResultList.Text; string fileName = ResultList.Text;
@@ -55,6 +60,8 @@ namespace Password_Manager
{ {
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
Close();
} }
} }
} }

View File

@@ -1,10 +1,11 @@
using System.Text; using System.Diagnostics;
using System.Text;
namespace Password_Generator 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(); StringBuilder builder = new StringBuilder();
Random rnd = new Random(); Random rnd = new Random();
@@ -30,5 +31,39 @@ namespace Password_Generator
return builder.ToString(); 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;
}
} }
} }

View File

@@ -61,7 +61,13 @@ namespace Password_Manager
} }
this.DataSource = elements; this.DataSource = elements;
SelectedIndex = 0; try
{
SelectedIndex = 0;
} catch (ArgumentOutOfRangeException)
{
SelectedIndex = -1;
}
} }
} }
} }