Skip to content

Commit

Permalink
gzip overload
Browse files Browse the repository at this point in the history
  • Loading branch information
Haik committed Mar 1, 2024
1 parent 3109033 commit 8945f33
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
38 changes: 38 additions & 0 deletions src/Pandatech.Crypto.Tests/GZipTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestClass>(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<TestClass>(invalidData));
Assert.NotNull(exception);
Assert.IsType<InvalidDataException>(exception);
}


[Fact]
public void Compress_String_ReturnsCompressedData()
{
Expand Down
1 change: 1 addition & 0 deletions src/Pandatech.Crypto.Tests/Pandatech.Crypto.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
Expand Down
22 changes: 20 additions & 2 deletions src/Pandatech.Crypto/GZip.cs
Original file line number Diff line number Diff line change
@@ -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>(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();
Expand All @@ -24,6 +33,15 @@ public static void Compress(Stream sourceStream, Stream destinationStream)
sourceStream.CopyTo(zipStream);
}

public static T? Decompress<T>(byte[] compressedData)
{
var decompressedData = Decompress(compressedData);

var jsonString = Encoding.UTF8.GetString(decompressedData);

return JsonSerializer.Deserialize<T>(jsonString);
}

public static byte[] Decompress(string compressedBase64)
{
var compressedData = Convert.FromBase64String(compressedBase64);
Expand Down
4 changes: 2 additions & 2 deletions src/Pandatech.Crypto/Pandatech.Crypto.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
<Copyright>MIT</Copyright>
<PackageIcon>pandatech.png</PackageIcon>
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<Version>2.2.9</Version>
<Version>2.2.10</Version>
<Title>Pandatech.Crypto</Title>
<PackageTags>Pandatech, library, encryption, hash, algorythms, security</PackageTags>
<Description>PandaTech.Crypto is a .NET library simplifying common cryptograhic functions.</Description>
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-pandatech-crypto</RepositoryUrl>
<PackageReleaseNotes>Overload for argon removal</PackageReleaseNotes>
<PackageReleaseNotes>Overload for Gzip</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 8945f33

Please sign in to comment.