Skip to content

Commit

Permalink
Merge pull request #16 from PandaTechAM/development
Browse files Browse the repository at this point in the history
Gzip underlying implementation change and Password symbols
  • Loading branch information
HaikAsatryan authored Mar 6, 2024
2 parents d9838de + 4f05ac7 commit 9765b56
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 48 deletions.
33 changes: 16 additions & 17 deletions src/Pandatech.Crypto.Tests/GZipTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,20 @@ public void Decompress_WithInvalidData_ShouldReturnNull()


[Fact]
public void Compress_String_ReturnsCompressedData()
public void CompressAndDecompress_String_ReturnsOriginalData()
{
// Arrange
const string input = "Hello, world!";
var expected = GZip.Compress(Encoding.UTF8.GetBytes(input));

// Act
var result = GZip.Compress(input);
var compressed = GZip.Compress(input);
var decompressedBytes = GZip.Decompress(compressed);

// Convert decompressed bytes back to string
var decompressedString = Encoding.UTF8.GetString(decompressedBytes);

// Assert
Assert.Equal(expected, result);
Assert.Equal(input, decompressedString);
}

[Fact]
Expand All @@ -76,35 +79,31 @@ public void Decompress_Base64_ReturnsOriginalData()
public void Compress_And_Decompress_Byte_Array_ReturnsOriginalData()
{
// Arrange
var input = Encoding.UTF8.GetBytes("Hello, world!");
var input = "Hello, world!";

// Act
var compressed = GZip.Compress(input);
var decompressed = GZip.Decompress(compressed);

// Assert
Assert.Equal(input, decompressed);
Assert.Equal(input, Encoding.UTF8.GetString(decompressed));
}

[Fact]
public void Compress_And_Decompress_Stream_ReturnsOriginalData()
public void CompressAndDecompress_ByteArray_ReturnsOriginalData()
{
// Arrange
var input = "Hello, world!"u8.ToArray();
using var inputStream = new MemoryStream(input);
using var compressedStream = new MemoryStream();
using var decompressedStream = new MemoryStream();

var input = Encoding.UTF8.GetBytes("Sample text for compression");

// Act
GZip.Compress(inputStream, compressedStream);
compressedStream.Seek(0, SeekOrigin.Begin);
GZip.Decompress(compressedStream, decompressedStream);
var result = decompressedStream.ToArray();
var compressed = GZip.Compress(input);
var decompressed = GZip.Decompress(compressed);

// Assert
Assert.Equal(input, result);
Assert.Equal(input, decompressed);
}


[Theory]
[InlineData("")]
[InlineData("Short string")]
Expand Down
59 changes: 31 additions & 28 deletions src/Pandatech.Crypto/GZip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,44 @@ 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);
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
var jsonString = JsonSerializer.Serialize(obj, options);
return Compress(jsonString);
}

public static byte[] Compress(string data)
{
return Compress(Encoding.UTF8.GetBytes(data));
using var memoryStream = new MemoryStream();
using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress))
{
using (var writer = new StreamWriter(gzipStream, Encoding.UTF8))
{
writer.Write(data);
}
}

var compressedData = memoryStream.ToArray();

return compressedData;
}

public static byte[] Compress(byte[] data)
{
using var compressedStream = new MemoryStream();
Compress(new MemoryStream(data), compressedStream);
return compressedStream.ToArray();
}
using var memoryStream = new MemoryStream();
using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress))
{
gzipStream.Write(data, 0, data.Length);
}

public static void Compress(Stream sourceStream, Stream destinationStream)
{
using var zipStream = new GZipStream(destinationStream, CompressionMode.Compress, leaveOpen: true);
sourceStream.CopyTo(zipStream);
return memoryStream.ToArray();
}

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

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

var jsonString = Decompress(compressedData);
return JsonSerializer.Deserialize<T>(jsonString);
}

Expand All @@ -50,14 +57,10 @@ public static byte[] Decompress(string compressedBase64)

public static byte[] Decompress(byte[] data)
{
using var decompressedStream = new MemoryStream();
Decompress(new MemoryStream(data), decompressedStream);
return decompressedStream.ToArray();
}

public static void Decompress(Stream sourceStream, Stream destinationStream)
{
using var zipStream = new GZipStream(sourceStream, CompressionMode.Decompress, leaveOpen: true);
zipStream.CopyTo(destinationStream);
using var compressedStream = new MemoryStream(data);
using var gzipStream = new GZipStream(compressedStream, CompressionMode.Decompress);
using var reader = new StreamReader(gzipStream, Encoding.UTF8);
var decompressedString = reader.ReadToEnd();
return Encoding.UTF8.GetBytes(decompressedString);
}
}
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.11</Version>
<Version>2.3.0</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 Gzip</PackageReleaseNotes>
<PackageReleaseNotes>Gzip underlying implementation change and Password symbols</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Pandatech.Crypto/Password.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public static class Password
private const string UppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private const string LowercaseChars = "abcdefghijklmnopqrstuvwxyz";
private const string DigitChars = "0123456789";
private const string SpecialChars = "!@#$%^*()-_=+[]{}|;:,.<>?";
private const string SpecialChars = "!@$*()-_=+[]{}|;:,.";

public static string GenerateRandom(int length, bool includeUppercase, bool includeLowercase, bool includeDigits,
bool includeSpecialChars)
Expand Down

0 comments on commit 9765b56

Please sign in to comment.