Moved common stuff to Common project

This commit is contained in:
2023-04-02 21:58:33 +02:00
parent dd10e5740e
commit f1206233a7
6 changed files with 0 additions and 0 deletions

View File

@@ -1,14 +0,0 @@
namespace Password_Manager
{
class Config
{
public string PasswordStorePath { get; set; }
public string Recipient { get; set; }
public Config(string PasswordStorePath, string Recipient)
{
this.PasswordStorePath = PasswordStorePath;
this.Recipient = Recipient;
}
}
}

View File

@@ -1,94 +0,0 @@
using Microsoft.VisualBasic.FileIO;
namespace Password_Manager
{
static class ConfigFileManager
{
#region Config flags
/*Configuration: (format: fieldname=value)
password_store_path: path to the folder which contains the .gpg files
recipient: whose public key to use for encryption when a new password is created
*/
private static string CONFIGPATH = SpecialDirectories.CurrentUserApplicationData + "\\config.cfg";
private static char DELIMETER = '=';
private static string RECIPIENT = "recipient";
private static string PATH = "password_store_path";
#endregion
private static Config Configuration;
public static void Init()
{
bool success = false;
while (!success)
{
if (File.Exists(CONFIGPATH))
{
StreamReader sr = new StreamReader(CONFIGPATH);
string? path = null, recipient = null;
while (!sr.EndOfStream)
{
string[]? fields = sr.ReadLine().Split(DELIMETER);
if (fields != null)
{
if (fields[0] == PATH)
{
path = fields[1];
}
else if (fields[0] == RECIPIENT)
{
recipient = fields[1];
}
}
else //probably an empty line or something
{
continue;
}
}
if (path != null && recipient != null)
{
Configuration = new Config(path, recipient);
success = true;
}
else
{
throw new InvalidConfigurationException("One or more required fileds were missing from the configuration file.");
}
}
else
{
CreateDefaultConfig();
}
}
}
public static string GetPath()
{
return Configuration.PasswordStorePath;
}
public static string GetRecipient()
{
return Configuration.Recipient;
}
private static void CreateDefaultConfig()
{
try
{
string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
StreamWriter sw = new StreamWriter(CONFIGPATH);
sw.WriteLine(RECIPIENT + DELIMETER + user);
sw.WriteLine(PATH + DELIMETER + SpecialDirectories.CurrentUserApplicationData);
sw.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}

View File

@@ -1,9 +0,0 @@
namespace Password_Manager
{
class InvalidConfigurationException : Exception
{
public InvalidConfigurationException() : base() { }
public InvalidConfigurationException(string message) : base(message) { }
public InvalidConfigurationException(string message, Exception inner) : base(message, inner) { }
}
}

View File

@@ -1,40 +0,0 @@
using Password_Manager;
using System.Text;
namespace Password_Generator
{
static class PasswordGenerator
{
private static string RandomStr(int length, bool no_symbols = false)
{
StringBuilder builder = new StringBuilder();
Random rnd = new Random();
for (int i = 0; i < length; i++)
{
if (rnd.Next(0, 101) <= 60) //60% chance for a character
{
char c = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * rnd.NextDouble() + 65))); //random character
if (no_symbols && Char.IsSymbol(c))
{
while (Char.IsSymbol(c))
{
c = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * rnd.NextDouble() + 65))); //random character
}
}
builder.Append(c);
}
else //40% change for number
{
builder.Append(Convert.ToChar(rnd.Next(0, 10)));
}
}
return builder.ToString();
}
public static string? New(string recipient, int length, bool no_symbols = false)
{
return new ProcessBuilder().GetOutput("cmd.exe", $"echo {RandomStr(length, no_symbols)} | gpg --quiet --encrypt --recipient {recipient}");
}
}
}

View File

@@ -1,61 +0,0 @@
using System.Diagnostics;
using System.Text;
namespace Password_Manager
{
public delegate void ProcessSuccess();
public delegate void ProcessFailure(Exception e);
sealed class ProcessBuilder
{
public event ProcessSuccess? ProcessFinished;
public event ProcessFailure? ProcessFailed;
public void Run(string procName, string args)
{
try
{
Process.Start(procName, args);
ProcessFinished?.Invoke();
}
catch (Exception e)
{
ProcessFailed?.Invoke(e);
}
}
public string? GetOutput(string procName, string args)
{
try
{
Process proc = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = procName,
Arguments = args,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
StringBuilder builder = new StringBuilder();
while (!proc.StandardOutput.EndOfStream)
{
builder.Append(proc.StandardOutput.ReadLine());
}
ProcessFinished?.Invoke();
return builder.ToString();
} catch (Exception e)
{
MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
ProcessFailed?.Invoke(e);
}
return null;
}
}
}

View File

@@ -1,36 +0,0 @@
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"