2022-02-09 00:44:56 +01:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
|
|
|
|
|
/* This work has been released under the CC0 1.0 Universal license! */
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2022-02-10 22:55:52 +01:00
|
|
|
|
using com.muldersoft.slunkcrypt.gui.utils;
|
|
|
|
|
|
2022-02-09 00:44:56 +01:00
|
|
|
|
namespace com.muldersoft.slunkcrypt.gui.process
|
|
|
|
|
{
|
|
|
|
|
public static class PasswordGen
|
|
|
|
|
{
|
2022-02-10 22:55:52 +01:00
|
|
|
|
private static readonly TimeSpan TIMEOUT = TimeSpan.FromSeconds(180);
|
2022-02-09 00:44:56 +01:00
|
|
|
|
|
2022-02-10 22:55:52 +01:00
|
|
|
|
private const string COMMAND_PASSWRD = "-p";
|
2022-02-09 00:44:56 +01:00
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
// Exception classes
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
public class GenerationFailedException : IOException
|
|
|
|
|
{
|
2022-02-10 22:55:52 +01:00
|
|
|
|
public GenerationFailedException(string message) : base(message)
|
2022-02-09 00:44:56 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
// Public methods
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
public static async Task<string> GeneratePassword(int length)
|
|
|
|
|
{
|
2022-02-10 22:55:52 +01:00
|
|
|
|
if (length <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException("Password length must be a positive value!");
|
|
|
|
|
}
|
2022-02-09 00:44:56 +01:00
|
|
|
|
using (FileStream executableFile = ExecutableHelper.GetExecutableFile())
|
|
|
|
|
{
|
2022-02-10 22:55:52 +01:00
|
|
|
|
string[] arguments = new string[] { COMMAND_PASSWRD, string.Format("{0:D}", length) };
|
|
|
|
|
Tuple<int, string[]> result = await ProcessRunner.ExecAsnyc(executableFile.Name, arguments, null, ProcessPriorityClass.BelowNormal, TIMEOUT);
|
|
|
|
|
if (result.Item1 == 0)
|
2022-02-09 00:44:56 +01:00
|
|
|
|
{
|
2022-02-10 22:55:52 +01:00
|
|
|
|
foreach (string password in result.Item2)
|
2022-02-09 00:44:56 +01:00
|
|
|
|
{
|
2022-02-10 22:55:52 +01:00
|
|
|
|
if ((password.Length >= length) && (!IsWeakPassword(password)))
|
|
|
|
|
{
|
|
|
|
|
return password;
|
|
|
|
|
}
|
2022-02-09 00:44:56 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-10 22:55:52 +01:00
|
|
|
|
throw new GenerationFailedException("Failed to generate password string!");
|
2022-02-09 00:44:56 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsWeakPassword(string password)
|
|
|
|
|
{
|
|
|
|
|
int flags = 0;
|
|
|
|
|
if (!string.IsNullOrEmpty(password))
|
|
|
|
|
{
|
|
|
|
|
foreach (char c in password)
|
|
|
|
|
{
|
|
|
|
|
if (char.IsLetter(c))
|
|
|
|
|
{
|
|
|
|
|
flags |= char.IsUpper(c) ? 0x2 : 0x1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
flags |= char.IsDigit(c) ? 0x8 : 0x4;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (flags != 0xF);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|