diff --git a/Azaion.Common/Extensions/StreamExtensions.cs b/Azaion.Common/Extensions/StreamExtensions.cs new file mode 100644 index 0000000..3fdd47f --- /dev/null +++ b/Azaion.Common/Extensions/StreamExtensions.cs @@ -0,0 +1,15 @@ +using System.Text; + +namespace Azaion.Common.Extensions; + +public static class StreamExtensions +{ + public static string ConvertToString(this Stream stream) + { + stream.Position = 0; + using var reader = new StreamReader(stream, Encoding.UTF8); + var result = reader.ReadToEnd(); + stream.Position = 0; + return result; + } +} \ No newline at end of file diff --git a/Azaion.Services/ResourcesService.cs b/Azaion.Services/ResourcesService.cs index bf1bde4..586134a 100644 --- a/Azaion.Services/ResourcesService.cs +++ b/Azaion.Services/ResourcesService.cs @@ -1,8 +1,5 @@ using Azaion.Common; using Azaion.Common.Configs; -using Azaion.Common.Database; -using Azaion.Common.Entities; -using LinqToDB; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; diff --git a/Azaion.Services/Security.cs b/Azaion.Services/Security.cs index d17c34d..a51114c 100644 --- a/Azaion.Services/Security.cs +++ b/Azaion.Services/Security.cs @@ -52,5 +52,6 @@ public static class Security int bytesRead; while ((bytesRead = await cryptoStream.ReadAsync(buffer, cancellationToken)) > 0) await toStream.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken); + toStream.Seek(0, SeekOrigin.Begin); } } diff --git a/Azaion.Test/SecurityTest.cs b/Azaion.Test/SecurityTest.cs index 51e833a..4e1308e 100644 --- a/Azaion.Test/SecurityTest.cs +++ b/Azaion.Test/SecurityTest.cs @@ -1,5 +1,6 @@ using System.Security.Cryptography; using System.Text; +using Azaion.Common.Extensions; using Azaion.Services; using FluentAssertions; using Newtonsoft.Json; @@ -27,7 +28,7 @@ public class SecurityTest await encryptedStream.DecryptTo(decryptedStream, key); encryptedStream.Close(); - var str = StreamToString(decryptedStream); + var str = decryptedStream.ConvertToString(); str.Should().Be(testString); } @@ -91,17 +92,9 @@ public class SecurityTest return stream; } - private static string StreamToString(Stream stream) - { - stream.Position = 0; - using var reader = new StreamReader(stream, Encoding.UTF8); - return reader.ReadToEnd(); - } - private static Stream StringToStream(string src) { var byteArray = Encoding.UTF8.GetBytes(src); return new MemoryStream(byteArray); } - } \ No newline at end of file