Skip to content

Commit 9c969d0

Browse files
Merge pull request #32 from PandaTechAM/development
Provided new overload methods without optional arguments
2 parents 87c82ff + 624626c commit 9c969d0

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

src/Pandatech.Crypto/Helpers/Aes256Siv.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,22 @@ internal static void RegisterKey(string key)
2020
GlobalKey = key;
2121
}
2222

23+
public static byte[] Encrypt(string plaintext)
24+
{
25+
return Encrypt(plaintext, null);
26+
}
27+
2328
public static byte[] Encrypt(string plaintext, string? key = null)
2429
{
2530
var bytes = Encoding.UTF8.GetBytes(plaintext);
2631
return Encrypt(bytes, key);
2732
}
2833

34+
public static byte[] Encrypt(byte[] plaintext)
35+
{
36+
return Encrypt(plaintext, null);
37+
}
38+
2939
public static byte[] Encrypt(byte[] plaintext, string? key = null)
3040
{
3141
if (plaintext.Length == 0)
@@ -42,6 +52,11 @@ public static byte[] Encrypt(byte[] plaintext, string? key = null)
4252
return Arrays.Concatenate(siv, cipher);
4353
}
4454

55+
public static void Encrypt(Stream input, Stream output)
56+
{
57+
Encrypt(input, output, null);
58+
}
59+
4560
public static void Encrypt(Stream input, Stream output, string? key = null)
4661
{
4762
ArgumentNullException.ThrowIfNull(input);
@@ -53,12 +68,22 @@ public static void Encrypt(Stream input, Stream output, string? key = null)
5368
output.Write(encrypted, 0, encrypted.Length);
5469
}
5570

71+
public static string Decrypt(byte[] ciphertext)
72+
{
73+
return Decrypt(ciphertext, null);
74+
}
75+
5676
public static string Decrypt(byte[] ciphertext, string? key = null)
5777
{
5878
var plain = DecryptToBytes(ciphertext, key);
5979
return Encoding.UTF8.GetString(plain);
6080
}
6181

82+
public static byte[] DecryptToBytes(byte[] ciphertext)
83+
{
84+
return DecryptToBytes(ciphertext, null);
85+
}
86+
6287
public static byte[] DecryptToBytes(byte[] ciphertext, string? key = null)
6388
{
6489
var keyBytes = GetKeyBytes(key);
@@ -86,6 +111,11 @@ public static byte[] DecryptToBytes(byte[] ciphertext, string? key = null)
86111
return plain;
87112
}
88113

114+
public static void Decrypt(Stream input, Stream output)
115+
{
116+
Decrypt(input, output, null);
117+
}
118+
89119
public static void Decrypt(Stream input, Stream output, string? key = null)
90120
{
91121
ArgumentNullException.ThrowIfNull(input);

src/Pandatech.Crypto/Pandatech.Crypto.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Copyright>MIT</Copyright>
99
<PackageIcon>pandatech.png</PackageIcon>
1010
<PackageReadmeFile>Readme.md</PackageReadmeFile>
11-
<Version>5.0.0</Version>
11+
<Version>5.0.1</Version>
1212
<Title>Pandatech.Crypto</Title>
1313
<PackageTags>Pandatech, library, encryption, hash, algorythms, security</PackageTags>
1414
<Description>PandaTech.Crypto is a .NET library simplifying common cryptograhic functions.</Description>

test/Pandatech.Crypto.Tests/Aes256SivTests.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,34 @@ private static string GenerateRandomAes256KeyString()
1212
return Random.GenerateAes256KeyString();
1313
}
1414

15+
16+
[Theory]
17+
[InlineData(" ")]
18+
[InlineData("HelloWorld")]
19+
[InlineData("Some special characters: ~!@#$%^&*()_+{}|:\"<>?")]
20+
[InlineData("Tabs\tNewLine\nCarriageReturn\rMixed\n\rAll")]
21+
[InlineData("Unicode test: 你好, мир, مرحبا, नमस्ते")]
22+
[InlineData("Emoji test: \U0001F600 \U0001F31F \U0001F680")]
23+
[InlineData("1234567890")]
24+
[InlineData("😀😃😄😁😆")]
25+
[InlineData("Line1\nLine2\r\nLine3")]
26+
[InlineData("A string with a null char \0 in between")]
27+
public void EncryptDecrypt_InlineData_WorksForAllKindOfStrings(string original)
28+
{
29+
// Arrange
30+
var key = GenerateRandomAes256KeyString();
31+
32+
// Act
33+
var encrypted = Aes256Siv.Encrypt(original, key);
34+
var decrypted = Aes256Siv.Decrypt(encrypted, key);
35+
36+
var encryptedBytesToString = Encoding.UTF8.GetString(encrypted);
37+
// Assert
38+
39+
Assert.NotEqual(original, encryptedBytesToString);
40+
Assert.Equal(original, decrypted);
41+
}
42+
1543
[Fact]
1644
public void EncryptDecrypt_ReturnsOriginalString()
1745
{
@@ -23,7 +51,7 @@ public void EncryptDecrypt_ReturnsOriginalString()
2351
var encrypted = Aes256Siv.Encrypt(original, key);
2452
var decrypted = Aes256Siv.Decrypt(encrypted, key);
2553

26-
54+
2755
Assert.Equal(original, decrypted);
2856
}
2957

0 commit comments

Comments
 (0)