This repository has been archived on 2025-09-26. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Password-Manager-Legacy/Password Manager/PasswordGenerator.cs

67 lines
2.3 KiB
C#
Raw Normal View History

using System.Text;
2023-03-28 12:53:28 +02:00
using System.Diagnostics;
2023-03-27 00:19:30 +02:00
namespace Password_Generator
{
2023-03-28 08:22:08 +02:00
static class PasswordGenerator
2023-03-27 00:19:30 +02:00
{
2023-03-28 08:22:08 +02:00
private static string RandomStr(int length, bool no_symbols = false)
2023-03-27 00:19:30 +02:00
{
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();
}
2023-03-28 08:22:08 +02:00
2023-03-28 12:53:16 +02:00
public static string? New(string recipient, int length, bool no_symbols = false)
2023-03-28 08:22:08 +02:00
{
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 = $"{RandomStr(length, no_symbols)} | gpg --quiet --encrypt --recipient {recipient}",
2023-03-28 08:22:08 +02:00
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
2023-03-28 10:28:15 +02:00
StringBuilder builder = new StringBuilder();
2023-03-28 08:22:08 +02:00
while (!proc.StandardOutput.EndOfStream)
{
2023-03-28 10:28:15 +02:00
builder.Append(proc.StandardOutput.ReadLine());
return builder.ToString();
2023-03-28 08:22:08 +02:00
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
2023-03-28 12:54:27 +02:00
return null;
2023-03-28 08:22:08 +02:00
}
2023-03-27 00:19:30 +02:00
}
}