mirror of
https://github.com/Stone-Red-Code/RemSox.git
synced 2026-09-04 00:56:19 +02:00
Code cleanup
This commit is contained in:
@@ -5,66 +5,65 @@ using Org.BouncyCastle.Crypto.Parameters;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace RemSox.Cryptography
|
||||
namespace RemSox.Cryptography;
|
||||
|
||||
public static class AesGcmCrypto
|
||||
{
|
||||
public static class AesGcmCrypto
|
||||
private const int NonceSize = 12;
|
||||
private const int TagSize = 128;
|
||||
|
||||
public static byte[] Encrypt(byte[] plainData, byte[] key)
|
||||
{
|
||||
private const int NonceSize = 12;
|
||||
private const int TagSize = 128;
|
||||
byte[] nonce = new byte[NonceSize];
|
||||
RandomNumberGenerator.Fill(nonce);
|
||||
|
||||
public static byte[] Encrypt(byte[] plainData, byte[] key)
|
||||
{
|
||||
byte[] nonce = new byte[NonceSize];
|
||||
RandomNumberGenerator.Fill(nonce);
|
||||
GcmBlockCipher cipher = new GcmBlockCipher(new AesEngine());
|
||||
cipher.Init(true, new AeadParameters(new KeyParameter(key), TagSize, nonce));
|
||||
|
||||
GcmBlockCipher cipher = new GcmBlockCipher(new AesEngine());
|
||||
cipher.Init(true, new AeadParameters(new KeyParameter(key), TagSize, nonce));
|
||||
byte[] output = new byte[cipher.GetOutputSize(plainData.Length)];
|
||||
|
||||
byte[] output = new byte[cipher.GetOutputSize(plainData.Length)];
|
||||
int len = cipher.ProcessBytes(plainData, 0, plainData.Length, output, 0);
|
||||
_ = cipher.DoFinal(output, len);
|
||||
|
||||
int len = cipher.ProcessBytes(plainData, 0, plainData.Length, output, 0);
|
||||
_ = cipher.DoFinal(output, len);
|
||||
byte[] result = new byte[nonce.Length + output.Length];
|
||||
|
||||
byte[] result = new byte[nonce.Length + output.Length];
|
||||
Buffer.BlockCopy(nonce, 0, result, 0, nonce.Length);
|
||||
Buffer.BlockCopy(output, 0, result, nonce.Length, output.Length);
|
||||
|
||||
Buffer.BlockCopy(nonce, 0, result, 0, nonce.Length);
|
||||
Buffer.BlockCopy(output, 0, result, nonce.Length, output.Length);
|
||||
return result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
public static string Encrypt(string text, byte[] key)
|
||||
{
|
||||
byte[] data = Encoding.UTF8.GetBytes(text);
|
||||
return Convert.ToBase64String(Encrypt(data, key));
|
||||
}
|
||||
|
||||
public static string Encrypt(string text, byte[] key)
|
||||
{
|
||||
byte[] data = Encoding.UTF8.GetBytes(text);
|
||||
return Convert.ToBase64String(Encrypt(data, key));
|
||||
}
|
||||
public static byte[] Decrypt(byte[] encryptedData, byte[] key)
|
||||
{
|
||||
byte[] nonce = new byte[NonceSize];
|
||||
Buffer.BlockCopy(encryptedData, 0, nonce, 0, NonceSize);
|
||||
|
||||
public static byte[] Decrypt(byte[] encryptedData, byte[] key)
|
||||
{
|
||||
byte[] nonce = new byte[NonceSize];
|
||||
Buffer.BlockCopy(encryptedData, 0, nonce, 0, NonceSize);
|
||||
int cipherLength = encryptedData.Length - NonceSize;
|
||||
byte[] cipherBytes = new byte[cipherLength];
|
||||
|
||||
int cipherLength = encryptedData.Length - NonceSize;
|
||||
byte[] cipherBytes = new byte[cipherLength];
|
||||
Buffer.BlockCopy(encryptedData, NonceSize, cipherBytes, 0, cipherLength);
|
||||
|
||||
Buffer.BlockCopy(encryptedData, NonceSize, cipherBytes, 0, cipherLength);
|
||||
GcmBlockCipher cipher = new GcmBlockCipher(new AesEngine());
|
||||
cipher.Init(false, new AeadParameters(new KeyParameter(key), TagSize, nonce));
|
||||
|
||||
GcmBlockCipher cipher = new GcmBlockCipher(new AesEngine());
|
||||
cipher.Init(false, new AeadParameters(new KeyParameter(key), TagSize, nonce));
|
||||
byte[] plain = new byte[cipher.GetOutputSize(cipherBytes.Length)];
|
||||
|
||||
byte[] plain = new byte[cipher.GetOutputSize(cipherBytes.Length)];
|
||||
int len = cipher.ProcessBytes(cipherBytes, 0, cipherBytes.Length, plain, 0);
|
||||
_ = cipher.DoFinal(plain, len);
|
||||
|
||||
int len = cipher.ProcessBytes(cipherBytes, 0, cipherBytes.Length, plain, 0);
|
||||
_ = cipher.DoFinal(plain, len);
|
||||
return plain;
|
||||
}
|
||||
|
||||
return plain;
|
||||
}
|
||||
|
||||
public static string Decrypt(string cipherText, byte[] key)
|
||||
{
|
||||
byte[] data = Convert.FromBase64String(cipherText);
|
||||
byte[] plain = Decrypt(data, key);
|
||||
return Encoding.UTF8.GetString(plain);
|
||||
}
|
||||
public static string Decrypt(string cipherText, byte[] key)
|
||||
{
|
||||
byte[] data = Convert.FromBase64String(cipherText);
|
||||
byte[] plain = Decrypt(data, key);
|
||||
return Encoding.UTF8.GetString(plain);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Threading.Tasks;
|
||||
using Cosmos.Kernel.System.Keyboard;
|
||||
using Cosmos.Kernel.System.Mouse;
|
||||
|
||||
using RemSox.Plugs;
|
||||
|
||||
using System.Drawing;
|
||||
|
||||
namespace RemSox.Cryptography;
|
||||
|
||||
public static class CryptoManager
|
||||
@@ -26,7 +23,10 @@ public static class CryptoManager
|
||||
|
||||
RandomNumberGeneratorImplementationImpl.AddMouseEntropy(dx, dy, dz, x, y);
|
||||
|
||||
if (!KeyboardManager.KeyAvailable) return;
|
||||
if (!KeyboardManager.KeyAvailable)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
KeyEvent keyEvent = KeyboardManager.Peek();
|
||||
|
||||
|
||||
@@ -3,25 +3,24 @@ using Org.BouncyCastle.Crypto.Digests;
|
||||
using Org.BouncyCastle.Crypto.Generators;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
namespace RemSox.Cryptography
|
||||
namespace RemSox.Cryptography;
|
||||
|
||||
public static class Pkcs5S2PasswordKeyDeriver
|
||||
{
|
||||
public static class Pkcs5S2PasswordKeyDeriver
|
||||
private const int KeySize = 32; // 256-bit key
|
||||
private const int Iterations = 150_000; // PBKDF2 cost factor
|
||||
|
||||
public static byte[] DeriveKey(string password, byte[] salt)
|
||||
{
|
||||
private const int KeySize = 32; // 256-bit key
|
||||
private const int Iterations = 150_000; // PBKDF2 cost factor
|
||||
Pkcs5S2ParametersGenerator generator = new Pkcs5S2ParametersGenerator(new Sha256Digest());
|
||||
|
||||
public static byte[] DeriveKey(string password, byte[] salt)
|
||||
{
|
||||
Pkcs5S2ParametersGenerator generator = new Pkcs5S2ParametersGenerator(new Sha256Digest());
|
||||
generator.Init(
|
||||
PbeParametersGenerator.Pkcs5PasswordToBytes(password.ToCharArray()),
|
||||
salt,
|
||||
Iterations
|
||||
);
|
||||
|
||||
generator.Init(
|
||||
PbeParametersGenerator.Pkcs5PasswordToBytes(password.ToCharArray()),
|
||||
salt,
|
||||
Iterations
|
||||
);
|
||||
|
||||
return ((KeyParameter)generator.GenerateDerivedMacParameters(KeySize * 8))
|
||||
.GetKey();
|
||||
}
|
||||
return ((KeyParameter)generator.GenerateDerivedMacParameters(KeySize * 8))
|
||||
.GetKey();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
global using Sys = Cosmos.Kernel.System;
|
||||
|
||||
using RemSox.Cryptography;
|
||||
using RemSox.Processes;
|
||||
using RemSox.Processing;
|
||||
using RemSox.UI.CLI;
|
||||
using RemSox.UI.CLI.Commands;
|
||||
|
||||
using System.Runtime;
|
||||
|
||||
namespace RemSox;
|
||||
@@ -34,8 +36,7 @@ public class Kernel : Sys.Kernel
|
||||
Sys.Mouse.MouseManager.Initialize();
|
||||
Sys.Mouse.MouseManager.SetScreenSize((int)canvas.Mode.Width, (int)canvas.Mode.Height);
|
||||
Sys.Keyboard.KeyboardManager.Initialize();
|
||||
|
||||
ProcessManager.SpawnProcess<CliProcess>();
|
||||
_ = ProcessManager.SpawnProcess<CliProcess>();
|
||||
}
|
||||
|
||||
protected override void Run()
|
||||
|
||||
@@ -1,48 +1,46 @@
|
||||
using System;
|
||||
using RemSox.Processing;
|
||||
using RemSox.Utils;
|
||||
|
||||
using YesNt.Interpreter.Runtime;
|
||||
using YesNt.Interpreter.Utilities;
|
||||
|
||||
namespace RemSox.Processes
|
||||
namespace RemSox.Processes;
|
||||
|
||||
public class YesNtInterpreterProcess() : Process("YesNtInterpreter")
|
||||
{
|
||||
public class YesNtInterpreterProcess() : Process("YesNtInterpreter")
|
||||
private YesNtInterpreter interpreter = null!;
|
||||
|
||||
internal override void Start(string[] args)
|
||||
{
|
||||
YesNtInterpreter interpreter = null!;
|
||||
// TODO: eventually we want to load these from a file instead of hardcoding them here
|
||||
args = [
|
||||
"win_create \"Test\" 320 240",
|
||||
"global winId = %win_last_id",
|
||||
"win_flush ${winId}",
|
||||
"print ${winId}",
|
||||
"label test:",
|
||||
"goto test"
|
||||
];
|
||||
interpreter = new();
|
||||
|
||||
internal override void Start(string[] args)
|
||||
YesNtWindowStatements.Register(interpreter, this);
|
||||
|
||||
interpreter.Prepare([.. args]);
|
||||
}
|
||||
|
||||
internal override void Tick()
|
||||
{
|
||||
if (interpreter.IsRunning)
|
||||
{
|
||||
// TODO: eventually we want to load these from a file instead of hardcoding them here
|
||||
args = [
|
||||
"win_create \"Test\" 320 240",
|
||||
"global winId = %win_last_id",
|
||||
"win_flush ${winId}",
|
||||
"print ${winId}",
|
||||
"label test:",
|
||||
"goto test"
|
||||
];
|
||||
interpreter = new();
|
||||
|
||||
YesNtWindowStatements.Register(interpreter, this);
|
||||
|
||||
interpreter.Prepare([.. args]);
|
||||
_ = interpreter.Step();
|
||||
}
|
||||
|
||||
internal override void Tick()
|
||||
else
|
||||
{
|
||||
if (interpreter.IsRunning)
|
||||
{
|
||||
interpreter.Step();
|
||||
}
|
||||
else
|
||||
{
|
||||
RequestStop();
|
||||
}
|
||||
}
|
||||
|
||||
internal override void Stop()
|
||||
{
|
||||
interpreter.Stop();
|
||||
RequestStop();
|
||||
}
|
||||
}
|
||||
|
||||
internal override void Stop()
|
||||
{
|
||||
interpreter.Stop();
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
using System.Drawing;
|
||||
using RemSox.Logging;
|
||||
using RemSox.Processing.IPC;
|
||||
using RemSox.UI.GUI.Windows;
|
||||
|
||||
using System.Drawing;
|
||||
|
||||
namespace RemSox.Processing;
|
||||
|
||||
public abstract class Process(string name)
|
||||
|
||||
@@ -216,7 +216,7 @@ public static class ProcessManager
|
||||
{
|
||||
process.Stop();
|
||||
|
||||
if (processes.TryRemove(process.Id, out var processEntry))
|
||||
if (processes.TryRemove(process.Id, out (Process Process, ProcessMetrics Metrics, TaskCompletionSource ExitSource) processEntry))
|
||||
{
|
||||
_ = processEntry.ExitSource.TrySetResult();
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ public static class CommandManager
|
||||
arguments = trimmedInput[commandName.Length..].TrimStart();
|
||||
}
|
||||
|
||||
cancellationToken.Register(async () => await entry.Value.StopAsync());
|
||||
CancellationTokenRegistration unused = cancellationToken.Register(async () => await entry.Value.StopAsync());
|
||||
|
||||
await entry.Value.ExecuteAsync(arguments, printLine);
|
||||
return true;
|
||||
|
||||
@@ -1,29 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using RemSox.Processing;
|
||||
|
||||
namespace RemSox.UI.CLI.Commands
|
||||
namespace RemSox.UI.CLI.Commands;
|
||||
|
||||
public class YesNtCommand : ICommand
|
||||
{
|
||||
public class YesNtCommand : ICommand
|
||||
public string Name => "yesnt";
|
||||
|
||||
public string Description => "Start the YesNt interpreter";
|
||||
|
||||
private int processId;
|
||||
|
||||
public async Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
public string Name => "yesnt";
|
||||
processId = ProcessManager.SpawnProcess<Processes.YesNtInterpreterProcess>(arguments?.Split(',') ?? []);
|
||||
await ProcessManager.WaitForProcessExitAsync(processId);
|
||||
}
|
||||
|
||||
public string Description => "Start the YesNt interpreter";
|
||||
|
||||
private int processId;
|
||||
|
||||
public async Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
processId = ProcessManager.SpawnProcess<Processes.YesNtInterpreterProcess>(arguments?.Split(',') ?? []);
|
||||
await ProcessManager.WaitForProcessExitAsync(processId);
|
||||
}
|
||||
|
||||
public Task StopAsync()
|
||||
{
|
||||
ProcessManager.StopProcess(processId);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
public Task StopAsync()
|
||||
{
|
||||
ProcessManager.StopProcess(processId);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
+4
-1
@@ -24,5 +24,8 @@ public interface ICommand
|
||||
/// <summary>
|
||||
/// Interrupts the command if it's currently running. This is called when the user presses Ctrl+C in the CLI.
|
||||
/// </summary>
|
||||
Task StopAsync() => Task.CompletedTask;
|
||||
Task StopAsync()
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
+609
-577
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user