Skip to content

Commit

Permalink
add password confirm dialogue
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminiserman committed Jan 26, 2022
1 parent 7a088d9 commit 893e1b0
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
30 changes: 28 additions & 2 deletions WingCryptWPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ private bool DoEncrypt()
return false;
}

if (!ConfirmPassword()) return false;

Mouse.OverrideCursor = Cursors.Wait;

string headerPath = ((TreeViewItem)fileTreeView.Items[0]).Header.ToString();
Expand All @@ -116,10 +118,11 @@ private void ButtonEncrypt_Click(object sender, RoutedEventArgs e)
}
finally
{
fileTreeView.Items.Clear();
Mouse.OverrideCursor = null;
}

fileTreeView.Items.Clear();

MessageBox.Show("Encryption completed.", "Encryption Complete", MessageBoxButton.OK, MessageBoxImage.None);
}

Expand Down Expand Up @@ -274,10 +277,11 @@ private void ButtonEncryptAndDelete_Click(object sender, RoutedEventArgs e)
}
finally
{
fileTreeView.Items.Clear();
Mouse.OverrideCursor = null;
}

fileTreeView.Items.Clear();

MessageBox.Show("Encryption completed.", "Encryption Complete", MessageBoxButton.OK, MessageBoxImage.None);
}

Expand Down Expand Up @@ -307,4 +311,26 @@ private void ButtonDecryptAndDelete_Click(object sender, RoutedEventArgs e)
Mouse.OverrideCursor = null;
}
}

private bool ConfirmPassword()
{
try
{
string confirm = PasswordDialog.GetPassword();

if (confirm == passwordTextBox.Password)
{
return true;
}
else
{
MessageBox.Show($"Passwords did not match. Please try again.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
}
catch
{
return false;
}
}
}
40 changes: 40 additions & 0 deletions WingCryptWPF/PasswordDialog.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Window x:Class="WingCryptWPF.PasswordDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WingCryptWPF"
mc:Ignorable="d"
Title="Confirm Password"
Height="75"
Width="325"
ResizeMode="CanMinimize"
SizeToContent="WidthAndHeight">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="5"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="220"/>
</Grid.ColumnDefinitions>

<TextBlock Grid.Column="0" Text="Confirm Password:" Padding="5,0" Margin="0, 5"/>
<PasswordBox Grid.Column="1" x:Name="passwordTextBox" Grid.Row="0" Margin="5,0" Height="20" Width="200"/>
</Grid>

<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="150"/>
</Grid.ColumnDefinitions>

<Button x:Name="confirmButton" Grid.Column="1" Margin="5, 5" Padding="5,0" Click="ButtonConfirm_Click">Confirm</Button>
</Grid>

</Grid>
</Window>
43 changes: 43 additions & 0 deletions WingCryptWPF/PasswordDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace WingCryptWPF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
/// <summary>
/// Interaction logic for PasswordDialog.xaml
/// </summary>
public partial class PasswordDialog : Window
{
public PasswordDialog()
{
InitializeComponent();
}

public string Password => passwordTextBox.Password;

public static string GetPassword()
{
PasswordDialog passwordDialog = new();

if (passwordDialog.ShowDialog() == true)
{
return passwordDialog.Password;
}
else throw new Exception("Cancelled.");
}

private void ButtonConfirm_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
}

0 comments on commit 893e1b0

Please sign in to comment.