vault unlocking WIP
This commit is contained in:
@@ -6,8 +6,5 @@ namespace Toolkit.Avalonia;
|
||||
public class AvaloniaDispatcher :
|
||||
IDispatcher
|
||||
{
|
||||
public async Task InvokeAsync(Action action)
|
||||
{
|
||||
await Dispatcher.UIThread.InvokeAsync(action);
|
||||
}
|
||||
public async Task Invoke(Action action) => await Dispatcher.UIThread.InvokeAsync(action);
|
||||
}
|
||||
@@ -79,7 +79,7 @@ public class ContentDialogHandler(IDispatcher dispatcher) :
|
||||
async void DeactivateHandler(object? sender, EventArgs args)
|
||||
{
|
||||
deactivatable.DeactivateHandler -= DeactivateHandler;
|
||||
await dispatcher.InvokeAsync(contentDialog.Hide);
|
||||
await dispatcher.Invoke(contentDialog.Hide);
|
||||
}
|
||||
|
||||
deactivatable.DeactivateHandler += DeactivateHandler;
|
||||
|
||||
@@ -7,7 +7,18 @@ public class AesDecryptor :
|
||||
{
|
||||
private const int IvSize = 16;
|
||||
|
||||
public byte[] Decrypt(byte[] cipher, byte[] key)
|
||||
public bool TryDecrypt(byte[] cipher,
|
||||
byte[] key,
|
||||
out byte[]? decryptedData)
|
||||
{
|
||||
decryptedData = null;
|
||||
|
||||
if (cipher is null || key is null || cipher.Length < IvSize)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Span<byte> iv = cipher.AsSpan(0, IvSize);
|
||||
ReadOnlySpan<byte> encryptedContent = cipher.AsSpan(IvSize);
|
||||
@@ -23,6 +34,17 @@ public class AesDecryptor :
|
||||
using MemoryStream resultStream = new();
|
||||
cryptoStream.CopyTo(resultStream);
|
||||
|
||||
return resultStream.ToArray();
|
||||
decryptedData = resultStream.ToArray();
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (CryptographicException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,20 +7,25 @@ public class AesEncryptor :
|
||||
{
|
||||
private const int IvSize = 16;
|
||||
|
||||
public byte[] Encrypt(byte[] data,
|
||||
byte[] key)
|
||||
public bool TryEncrypt(byte[] data,
|
||||
byte[] key,
|
||||
out byte[]? encryptedData)
|
||||
{
|
||||
if (key.Length != 32)
|
||||
encryptedData = null;
|
||||
|
||||
if (data is null || key is null || key.Length != 32)
|
||||
{
|
||||
throw new ArgumentException("Key must be 256 bits (32 bytes).");
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using Aes aes = Aes.Create();
|
||||
aes.Key = key;
|
||||
aes.GenerateIV();
|
||||
|
||||
using MemoryStream memoryStream = new();
|
||||
memoryStream.Write(aes.IV.AsSpan(0, IvSize));
|
||||
memoryStream.Write(aes.IV, 0, IvSize);
|
||||
|
||||
using (ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
|
||||
using (CryptoStream cryptoStream = new(memoryStream, encryptor, CryptoStreamMode.Write))
|
||||
@@ -29,6 +34,16 @@ public class AesEncryptor :
|
||||
cryptoStream.FlushFinalBlock();
|
||||
}
|
||||
|
||||
return memoryStream.ToArray();
|
||||
encryptedData = memoryStream.ToArray();
|
||||
return true;
|
||||
}
|
||||
catch (CryptographicException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
namespace Toolkit.Foundation;
|
||||
public interface IDecryptor
|
||||
{
|
||||
byte[] Decrypt(byte[] cipher,
|
||||
byte[] key);
|
||||
bool TryDecrypt(byte[] cipher,
|
||||
byte[] key,
|
||||
out byte[]? decryptedData);
|
||||
}
|
||||
@@ -2,5 +2,5 @@
|
||||
|
||||
public interface IDispatcher
|
||||
{
|
||||
Task InvokeAsync(Action action);
|
||||
Task Invoke(Action action);
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IEncryptor
|
||||
{
|
||||
byte[] Encrypt(byte[] data, byte[] key);
|
||||
bool TryEncrypt(byte[] data,
|
||||
byte[] key,
|
||||
out byte[]? encryptedData);
|
||||
}
|
||||
@@ -412,6 +412,17 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
||||
CollectionChanged?.Invoke(this, args);
|
||||
}
|
||||
|
||||
public partial class ObservableCollectionViewModel<TValue, TViewModel>(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber, IDisposer disposer) : ObservableCollectionViewModel<TViewModel>(provider, factory, mediator, publisher, subscriber, disposer)
|
||||
where TViewModel : notnull
|
||||
{
|
||||
[ObservableProperty]
|
||||
private TValue? value;
|
||||
}
|
||||
|
||||
public class ObservableCollectionViewModel(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
|
||||
@@ -73,29 +73,29 @@ public class Publisher(ISubscriptionManager subscriptionManager,
|
||||
public Task PublishUI<TMessage>(object key,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TMessage : new() =>
|
||||
Publish(new TMessage(), args => dispatcher.InvokeAsync(async () => await args()),
|
||||
Publish(new TMessage(), args => dispatcher.Invoke(async () => await args()),
|
||||
key, cancellationToken);
|
||||
|
||||
public Task PublishUI<TMessage>(TMessage message,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TMessage : notnull =>
|
||||
Publish(message, args => dispatcher.InvokeAsync(async () => await args()),
|
||||
Publish(message, args => dispatcher.Invoke(async () => await args()),
|
||||
null, cancellationToken);
|
||||
|
||||
public Task PublishUI<TMessage>(TMessage message,
|
||||
object key,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TMessage : notnull =>
|
||||
Publish(message, args => dispatcher.InvokeAsync(async () => await args()),
|
||||
Publish(message, args => dispatcher.Invoke(async () => await args()),
|
||||
key, cancellationToken);
|
||||
|
||||
public Task PublishUI<TMessage>(CancellationToken cancellationToken = default)
|
||||
where TMessage : new() =>
|
||||
Publish(new TMessage(), args => dispatcher.InvokeAsync(async () => await args()),
|
||||
Publish(new TMessage(), args => dispatcher.Invoke(async () => await args()),
|
||||
null, cancellationToken);
|
||||
|
||||
public Task PublishUI(object message,
|
||||
CancellationToken cancellationToken = default) => Publish(message, args =>
|
||||
dispatcher.InvokeAsync(async () => await args()),
|
||||
dispatcher.Invoke(async () => await args()),
|
||||
null, cancellationToken);
|
||||
}
|
||||
Reference in New Issue
Block a user