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/Common/PasswordGenerator.cs

50 lines
1.7 KiB
C#
Raw Permalink Normal View History

2023-04-02 22:04:30 +02:00
using System.Text;
2023-03-31 07:29:07 +02:00
2023-04-02 22:10:42 +02:00
namespace Common;
2023-04-02 22:28:01 +02:00
public static class PasswordGenerator
2023-03-27 00:19:30 +02:00
{
2023-04-02 22:40:47 +02:00
public static Error? ExceptionOccured;
public static int DEFAULT_LENGTH = 16;
private static string RandomStr(int length = 16, bool no_symbols = false)
2023-03-27 00:19:30 +02:00
{
2023-04-02 22:10:42 +02:00
StringBuilder builder = new StringBuilder();
Random rnd = new Random();
for (int i = 0; i < length; i++)
2023-03-27 00:19:30 +02:00
{
2023-04-02 22:10:42 +02:00
if (rnd.Next(0, 101) <= 60) //60% chance for a character
2023-03-27 00:19:30 +02:00
{
2023-04-02 22:10:42 +02:00
char c = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * rnd.NextDouble() + 65))); //random character
if (no_symbols && Char.IsSymbol(c))
2023-03-27 00:19:30 +02:00
{
2023-04-02 22:10:42 +02:00
while (Char.IsSymbol(c))
2023-03-27 00:19:30 +02:00
{
2023-04-02 22:10:42 +02:00
c = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * rnd.NextDouble() + 65))); //random character
2023-03-27 00:19:30 +02:00
}
}
2023-04-02 22:10:42 +02:00
builder.Append(c);
}
else //40% change for number
{
builder.Append(Convert.ToChar(rnd.Next(0, 10)));
2023-03-27 00:19:30 +02:00
}
}
2023-03-28 08:22:08 +02:00
2023-04-02 22:10:42 +02:00
return builder.ToString();
}
public static string? New(string recipient, int length, bool no_symbols = false)
{
2023-04-02 22:40:47 +02:00
ProcessBuilder pb = new ProcessBuilder();
pb.ProcessFailed += (e) => ExceptionOccured?.Invoke(e);
2023-04-16 20:43:04 +02:00
// if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
// {
2023-04-17 16:45:50 +02:00
return pb.GetOutput("cmd.exe", $"echo {RandomStr(length, no_symbols)} | gpg --quiet --encrypt --recipient {recipient}");
2023-04-16 20:43:04 +02:00
// }
// else
// {
// return pb.GetOutput("echo", $"{RandomStr(length, no_symbols)} | gpg --quiet --encrypt --recipient {recipient}");
// }
2023-03-27 00:19:30 +02:00
}
}