diff --git a/src/Pandatech.Crypto.Tests/GZipTests.cs b/src/Pandatech.Crypto.Tests/GZipTests.cs index ee48377..9b22a17 100644 --- a/src/Pandatech.Crypto.Tests/GZipTests.cs +++ b/src/Pandatech.Crypto.Tests/GZipTests.cs @@ -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] @@ -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")] diff --git a/src/Pandatech.Crypto/GZip.cs b/src/Pandatech.Crypto/GZip.cs index c2d4329..4902aff 100644 --- a/src/Pandatech.Crypto/GZip.cs +++ b/src/Pandatech.Crypto/GZip.cs @@ -8,37 +8,44 @@ public static class GZip { public static byte[] Compress(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(byte[] compressedData) { - var decompressedData = Decompress(compressedData); - - var jsonString = Encoding.UTF8.GetString(decompressedData); - + var jsonString = Decompress(compressedData); return JsonSerializer.Deserialize(jsonString); } @@ -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); } } \ No newline at end of file diff --git a/src/Pandatech.Crypto/Pandatech.Crypto.csproj b/src/Pandatech.Crypto/Pandatech.Crypto.csproj index ea5b43c..bb006eb 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.11 + 2.3.0 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 Gzip + Gzip underlying implementation change and Password symbols diff --git a/src/Pandatech.Crypto/Password.cs b/src/Pandatech.Crypto/Password.cs index 629ea1a..76244fb 100644 --- a/src/Pandatech.Crypto/Password.cs +++ b/src/Pandatech.Crypto/Password.cs @@ -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)