diff --git a/Password Manager/MainForm.Designer.cs b/Password Manager/MainForm.Designer.cs index 042c779..c355e01 100644 --- a/Password Manager/MainForm.Designer.cs +++ b/Password Manager/MainForm.Designer.cs @@ -29,47 +29,72 @@ private void InitializeComponent() { SearchBox = new TextBox(); - ResultList = new PasswordListBox(); GeneratePassword = new Button(); + ResultList = new PasswordListBox(PathRequest); + DecryptBtn = new Button(); + Cancel = new Button(); SuspendLayout(); // - // searchBox + // SearchBox // SearchBox.Location = new Point(10, 20); SearchBox.Margin = new Padding(3, 2, 3, 2); - SearchBox.Name = "searchBox"; + SearchBox.Name = "SearchBox"; SearchBox.PlaceholderText = "Search for a password"; SearchBox.Size = new Size(225, 23); SearchBox.TabIndex = 0; - SearchBox.TextChanged += ResultList.ReloadResults; + SearchBox.TextChanged += ReloadResults; // - // resultList - // - ResultList.FormattingEnabled = true; - ResultList.ItemHeight = 15; - ResultList.Location = new Point(10, 45); - ResultList.Margin = new Padding(3, 2, 3, 2); - ResultList.Name = "resultList"; - ResultList.Size = new Size(225, 274); - ResultList.TabIndex = 1; - // - // button1 + // GeneratePassword // GeneratePassword.Location = new Point(241, 20); - GeneratePassword.Name = "button1"; + GeneratePassword.Name = "GeneratePassword"; GeneratePassword.Size = new Size(75, 23); GeneratePassword.TabIndex = 5; GeneratePassword.Text = "Generate"; GeneratePassword.UseVisualStyleBackColor = true; GeneratePassword.Click += OpenPasswordGenerator; // + // ResultList + // + ResultList.ColumnWidth = 20; + ResultList.FormattingEnabled = true; + ResultList.HorizontalScrollbar = true; + ResultList.ItemHeight = 15; + ResultList.Location = new Point(10, 48); + ResultList.Name = "ResultList"; + ResultList.Size = new Size(306, 274); + ResultList.TabIndex = 6; + // + // DecryptBtn + // + DecryptBtn.Location = new Point(241, 328); + DecryptBtn.Name = "DecryptBtn"; + DecryptBtn.Size = new Size(75, 23); + DecryptBtn.TabIndex = 7; + DecryptBtn.Text = "Decrypt"; + DecryptBtn.UseVisualStyleBackColor = true; + DecryptBtn.Click += Decrypt; + // + // Cancel + // + Cancel.Location = new Point(160, 328); + Cancel.Name = "Cancel"; + Cancel.Size = new Size(75, 23); + Cancel.TabIndex = 8; + Cancel.Text = "Cancel"; + Cancel.UseVisualStyleBackColor = true; + // // MainForm // + AcceptButton = DecryptBtn; AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(700, 338); - Controls.Add(GeneratePassword); + ClientSize = new Size(324, 374); + Controls.Add(Cancel); + Controls.Add(DecryptBtn); Controls.Add(ResultList); + Controls.Add(GeneratePassword); Controls.Add(SearchBox); Margin = new Padding(3, 2, 3, 2); Name = "MainForm"; @@ -81,7 +106,9 @@ #endregion private TextBox SearchBox; - private PasswordListBox ResultList; private Button GeneratePassword; + private PasswordListBox ResultList; + private Button DecryptBtn; + private Button Cancel; } } \ No newline at end of file diff --git a/Password Manager/MainForm.cs b/Password Manager/MainForm.cs index 7d43845..a682d5e 100644 --- a/Password Manager/MainForm.cs +++ b/Password Manager/MainForm.cs @@ -1,3 +1,5 @@ +using System.Diagnostics; + namespace Password_Manager { sealed public partial class MainForm : Form @@ -9,7 +11,12 @@ namespace Password_Manager this.PathRequest = PathRequest; InitializeComponent(); - ResultList = new PasswordListBox(PathRequest); + ResultList.SearchQueryRequest += () => SearchBox.Text; + ResultList.ReloadResults(); + } + + private void ReloadResults(object? sender, EventArgs? e) //proxy method for SearchBox.TextChanged + { ResultList.ReloadResults(); } @@ -18,5 +25,36 @@ namespace Password_Manager GeneratePassword gp = new GeneratePassword(SearchBox.Text, PathRequest()); gp.ShowDialog(); } + + private void Decrypt(object sender, EventArgs e) + { + string fileName = ResultList.Text; + try + { + Process proc = new Process() + { + StartInfo = new ProcessStartInfo + { + FileName = "gpg.exe", + Arguments = $"--quiet --decrypt {PathRequest()}\\{fileName}", + 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); + } + } } } \ No newline at end of file diff --git a/Password Manager/PasswordListBox.cs b/Password Manager/PasswordListBox.cs index bf50d17..690abbd 100644 --- a/Password Manager/PasswordListBox.cs +++ b/Password Manager/PasswordListBox.cs @@ -1,4 +1,4 @@ -using System.Runtime.CompilerServices; +using System.Collections; namespace Password_Manager { @@ -31,22 +31,23 @@ namespace Password_Manager } else { - throw new InvalidOperationException("You cannot use this method if you instantiated the object using the parameterless constructor"); + throw new InvalidOperationException("You cannot use the ReloadResults method if you instantiated the object using the parameterless constructor"); } files = d.GetFiles("*.gpg"); - } catch (ArgumentNullException e) + } + catch (ArgumentNullException e) { MessageBox.Show(e.ToString(), "Error: Invalid path", MessageBoxButtons.OK, MessageBoxIcon.Error); } - string[] arrayTmp = new string[files.Length]; - for (int i = 0; i < arrayTmp.Length; i++) + List elements = new List(); + for (int i = 0; i < files.Length; i++) { - arrayTmp[i] = files[i].Name; + elements.Add(files[i].Name); } - List elements = new List(arrayTmp); string[] copy = elements.ToArray(); + string? searchQuery = SearchQueryRequest?.Invoke(); if (searchQuery != null) //we have a search query { @@ -60,6 +61,7 @@ namespace Password_Manager } this.DataSource = elements; + SelectedIndex = 0; } } } diff --git a/Password Manager/ToastNotification.ps1 b/Password Manager/ToastNotification.ps1 new file mode 100644 index 0000000..fbbf392 --- /dev/null +++ b/Password Manager/ToastNotification.ps1 @@ -0,0 +1,36 @@ +function Show-Notification { + [cmdletbinding()] + Param ( + [string] + $ToastTitle, + [string] + [parameter(ValueFromPipeline)] + $ToastText + ) + + [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null + $Template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02) + + $RawXml = [xml] $Template.GetXml() + ($RawXml.toast.visual.binding.text | Where-Object {$_.id -eq "1"}).AppendChild($RawXml.CreateTextNode($ToastTitle)) > $null + ($RawXml.toast.visual.binding.text | Where-Object {$_.id -eq "2"}).AppendChild($RawXml.CreateTextNode($ToastText)) > $null + + $SerializedXml = New-Object Windows.Data.Xml.Dom.XmlDocument + $SerializedXml.LoadXml($RawXml.OuterXml) + + $Toast = [Windows.UI.Notifications.ToastNotification]::new($SerializedXml) + $Toast.Tag = "PowerShell" + $Toast.Group = "PowerShell" + $Toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(1) + + $Notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("Password Manager") + $Notifier.Show($Toast); +} + +param( + [Parameter(Mandatory)] + [String]$toastTitle, + [String]$toastText +) + +Show-Notification -ToastTitle "$toastTitle" -ToastText "$toastText" \ No newline at end of file