patbef-ServiceOutside/ServiceShared/Crypto/ProtectedServiceRun.cs

83 lines
2.8 KiB
C#
Raw Normal View History

2024-01-29 16:27:34 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace ServiceShared.Crypto
{
public static class ProtectedServiceRun
{
/// <summary>
/// Verifies the SecureHash by password
/// </summary>
/// <param name="secureHash">secure hash from the appsettings.json</param>
/// <returns></returns>
public static bool Verified(string secureHash)
{
bool result = false;
if(string.IsNullOrEmpty(secureHash))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("SecureHash was not found in the appsettings.json");
Console.WriteLine("Please encrypt the string \"ServiceOutside\" with ServiceShared.Crypto.AES.Encryption method and store the value by SecureHash in the appsettings.json");
Console.ForegroundColor = ConsoleColor.White;
}
else
{
bool password_successfully = false;
bool exited = false;
string password = "";
ConsoleKeyInfo key;
while (!password_successfully && !exited)
{
Console.Write("Please enter your master password: ");
do
{
key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Escape)
{
exited = true;
break;
}
else if (key.Key == ConsoleKey.Enter)
{
byte[] aesKey = AES.GetKey(password);
string decrypted = AES.Decrypt(secureHash, aesKey);
if(!string.IsNullOrEmpty(decrypted) && decrypted == "SecureHash")
{
AES.SetMemoryMasterKey(password);
password_successfully = true;
result = true;
break;
}
else
{
password = "";
Console.Clear();
break;
}
}
else
{
password += key.KeyChar;
Console.Write("*");
}
}
while (key.Key != ConsoleKey.Escape && key.Key != ConsoleKey.Escape);
}
}
Console.Clear();
return result;
}
}
}