From 8945f334427cdbdbe64802bb8d8fb522bb8b457d Mon Sep 17 00:00:00 2001 From: Haik Date: Fri, 1 Mar 2024 17:45:48 +0400 Subject: [PATCH] gzip overload --- src/Pandatech.Crypto.Tests/GZipTests.cs | 38 +++++++++++++++++++ .../Pandatech.Crypto.Tests.csproj | 1 + src/Pandatech.Crypto/GZip.cs | 22 ++++++++++- src/Pandatech.Crypto/Pandatech.Crypto.csproj | 4 +- 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/src/Pandatech.Crypto.Tests/GZipTests.cs b/src/Pandatech.Crypto.Tests/GZipTests.cs index 1491175..ee48377 100644 --- a/src/Pandatech.Crypto.Tests/GZipTests.cs +++ b/src/Pandatech.Crypto.Tests/GZipTests.cs @@ -4,6 +4,44 @@ namespace Pandatech.Crypto.Tests; public class GZipTests { + private class TestClass + { + public int Id { get; init; } + public string? Name { get; init; } + } + + [Fact] + public void CompressAndDecompress_ShouldReturnOriginalObject() + { + var originalObject = new TestClass + { + Id = 1, + Name = "Test" + }; + + // Act + var compressedData = GZip.Compress(originalObject); + var decompressedObject = GZip.Decompress(compressedData); + + // Assert + Assert.NotNull(decompressedObject); + Assert.Equal(originalObject.Id, decompressedObject.Id); + Assert.Equal(originalObject.Name, decompressedObject.Name); + } + + [Fact] + public void Decompress_WithInvalidData_ShouldReturnNull() + { + // Arrange + var invalidData = Encoding.UTF8.GetBytes("Invalid compressed data"); + + // Act & Assert + var exception = Record.Exception(() => GZip.Decompress(invalidData)); + Assert.NotNull(exception); + Assert.IsType(exception); + } + + [Fact] public void Compress_String_ReturnsCompressedData() { diff --git a/src/Pandatech.Crypto.Tests/Pandatech.Crypto.Tests.csproj b/src/Pandatech.Crypto.Tests/Pandatech.Crypto.Tests.csproj index 14eb77f..1e0d0e2 100644 --- a/src/Pandatech.Crypto.Tests/Pandatech.Crypto.Tests.csproj +++ b/src/Pandatech.Crypto.Tests/Pandatech.Crypto.Tests.csproj @@ -10,6 +10,7 @@ + diff --git a/src/Pandatech.Crypto/GZip.cs b/src/Pandatech.Crypto/GZip.cs index 7b0c01f..c2d4329 100644 --- a/src/Pandatech.Crypto/GZip.cs +++ b/src/Pandatech.Crypto/GZip.cs @@ -1,16 +1,25 @@ using System.IO.Compression; using System.Text; +using System.Text.Json; namespace Pandatech.Crypto; public static class GZip { - + public static byte[] Compress(T obj) + { + var jsonString = JsonSerializer.Serialize(obj); + + var jsonData = Encoding.UTF8.GetBytes(jsonString); + + return Compress(jsonData); + } + public static byte[] Compress(string data) { return Compress(Encoding.UTF8.GetBytes(data)); } - + public static byte[] Compress(byte[] data) { using var compressedStream = new MemoryStream(); @@ -24,6 +33,15 @@ public static void Compress(Stream sourceStream, Stream destinationStream) sourceStream.CopyTo(zipStream); } + public static T? Decompress(byte[] compressedData) + { + var decompressedData = Decompress(compressedData); + + var jsonString = Encoding.UTF8.GetString(decompressedData); + + return JsonSerializer.Deserialize(jsonString); + } + public static byte[] Decompress(string compressedBase64) { var compressedData = Convert.FromBase64String(compressedBase64); diff --git a/src/Pandatech.Crypto/Pandatech.Crypto.csproj b/src/Pandatech.Crypto/Pandatech.Crypto.csproj index 7e1832c..861725b 100644 --- a/src/Pandatech.Crypto/Pandatech.Crypto.csproj +++ b/src/Pandatech.Crypto/Pandatech.Crypto.csproj @@ -8,12 +8,12 @@ MIT pandatech.png Readme.md - 2.2.9 + 2.2.10 Pandatech.Crypto Pandatech, library, encryption, hash, algorythms, security PandaTech.Crypto is a .NET library simplifying common cryptograhic functions. https://github.com/PandaTechAM/be-lib-pandatech-crypto - Overload for argon removal + Overload for Gzip