fix encryption

This commit is contained in:
Alex Bezdieniezhnykh
2024-11-09 07:25:47 +02:00
parent 121052a3ef
commit ca6175da7f
3 changed files with 75 additions and 13 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ public class ResourcesService(IOptions<ResourcesConfig> resourcesConfig) : IReso
{
var fileStream = new FileStream(GetResourcePath(request.ResourceEnum), FileMode.Open, FileAccess.Read);
var key = Security.MakeEncryptionKey(request.Username, request.Password);
await fileStream.Encrypt(outputStream, key, cancellationToken);
await fileStream.EncryptTo(outputStream, key, cancellationToken);
}
public async Task SaveResource(UploadResourceRequest request, CancellationToken cancellationToken = default)
+8 -8
View File
@@ -5,7 +5,7 @@ namespace Azaion.Services;
public static class Security
{
private const int BUFFER_SIZE = 81920; // 80 KB buffer size
private const int BUFFER_SIZE = 524288; // 512 KB buffer size
public static string ToHash(this string str) =>
Convert.ToBase64String(SHA384.HashData(Encoding.UTF8.GetBytes(str)));
@@ -13,9 +13,9 @@ public static class Security
public static string MakeEncryptionKey(string username, string password) =>
$"{username}-{password}---#%@AzaionKey@%#---";
public static async Task Encrypt(this Stream stream, Stream outputStream, string key, CancellationToken cancellationToken = default)
public static async Task EncryptTo(this Stream stream, Stream toStream, string key, CancellationToken cancellationToken = default)
{
if (stream is { CanSeek: false }) throw new ArgumentNullException(nameof(stream));
if (stream is { CanRead: false }) throw new ArgumentNullException(nameof(stream));
if (key is not { Length: > 0 }) throw new ArgumentNullException(nameof(key));
using var aes = Aes.Create();
@@ -23,10 +23,10 @@ public static class Security
aes.GenerateIV();
using var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
await using var cs = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write);
await using var cs = new CryptoStream(toStream, encryptor, CryptoStreamMode.Write, leaveOpen: true);
// Prepend IV to the encrypted data
await outputStream.WriteAsync(aes.IV.AsMemory(0, aes.IV.Length), cancellationToken);
await toStream.WriteAsync(aes.IV.AsMemory(0, aes.IV.Length), cancellationToken);
var buffer = new byte[BUFFER_SIZE];
int bytesRead;
@@ -34,7 +34,7 @@ public static class Security
await cs.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken);
}
public static async Task Decrypt(this Stream encryptedStream, Stream outputStream, string key, CancellationToken cancellationToken = default)
public static async Task DecryptTo(this Stream encryptedStream, Stream toStream, string key, CancellationToken cancellationToken = default)
{
using var aes = Aes.Create();
aes.Key = SHA256.HashData(Encoding.UTF8.GetBytes(key));
@@ -45,12 +45,12 @@ public static class Security
aes.IV = iv;
using var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
await using var cryptoStream = new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read);
await using var cryptoStream = new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read, leaveOpen: true);
// Read and write in chunks
var buffer = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = await cryptoStream.ReadAsync(buffer, cancellationToken)) > 0)
await outputStream.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken);
await toStream.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken);
}
}