Skip to content

Commit 501d736

Browse files
author
Vadim Belov
committed
Rename string encrypt/decrypt methods; add byte[] overloads
Renamed Encrypt/Decrypt to EncryptString/DecryptString for clarity. Added Encrypt and Decrypt extension methods for byte arrays. All methods use MemoryStream and await async cipher operations.
1 parent 7abc255 commit 501d736

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

Sources/EasyExtensions/Extensions/StreamCipherExtensions.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static class StreamCipherExtensions
2020
/// <param name="plainText">The plain text to encrypt. Cannot be null.</param>
2121
/// <returns>A byte array containing the encrypted representation of the input plain text.</returns>
2222
/// <exception cref="ArgumentNullException">Thrown if <paramref name="plainText"/> is null.</exception>
23-
public static byte[] Encrypt(this IStreamCipher streamCipher, string plainText)
23+
public static byte[] EncryptString(this IStreamCipher streamCipher, string plainText)
2424
{
2525
if (plainText == null)
2626
{
@@ -41,7 +41,7 @@ public static byte[] Encrypt(this IStreamCipher streamCipher, string plainText)
4141
/// <param name="cipherBytes">The encrypted data to decrypt, as a byte array. Cannot be null.</param>
4242
/// <returns>A string containing the decrypted plaintext, decoded from UTF-8.</returns>
4343
/// <exception cref="ArgumentNullException">Thrown if cipherBytes is null.</exception>
44-
public static string Decrypt(this IStreamCipher streamCipher, byte[] cipherBytes)
44+
public static string DecryptString(this IStreamCipher streamCipher, byte[] cipherBytes)
4545
{
4646
if (cipherBytes == null)
4747
{
@@ -53,5 +53,40 @@ public static string Decrypt(this IStreamCipher streamCipher, byte[] cipherBytes
5353
byte[] plainBytes = outputStream.ToArray();
5454
return Encoding.UTF8.GetString(plainBytes);
5555
}
56+
57+
/// <summary>
58+
/// Encrypts the specified plain text using the provided stream cipher and returns the encrypted data as a byte
59+
/// array.
60+
/// </summary>
61+
/// <param name="streamCipher">The stream cipher to use for encrypting the plain text. Must not be null.</param>
62+
/// <param name="plainBytes">The plain text to encrypt, as a byte array. Cannot be null.</param>
63+
/// <returns>A byte array containing the encrypted representation of the input plain text.</returns>
64+
public static byte[] Encrypt(this IStreamCipher streamCipher, byte[] plainBytes)
65+
{
66+
using var inputStream = new System.IO.MemoryStream(plainBytes);
67+
using var outputStream = new System.IO.MemoryStream();
68+
streamCipher.EncryptAsync(inputStream, outputStream).GetAwaiter().GetResult();
69+
return outputStream.ToArray();
70+
}
71+
72+
/// <summary>
73+
/// Decrypts the specified byte array using the provided stream cipher and returns the resulting plaintext as a
74+
/// UTF-8 encoded string.
75+
/// </summary>
76+
/// <param name="streamCipher">The stream cipher to use for decryption. Must implement the IStreamCipher interface.</param>
77+
/// <param name="cipherBytes">The encrypted data to decrypt, as a byte array. Cannot be null.</param>
78+
/// <returns>A byte array containing the decrypted plaintext.</returns>
79+
/// <exception cref="ArgumentNullException">Thrown if cipherBytes is null.</exception>
80+
public static byte[] Decrypt(this IStreamCipher streamCipher, byte[] cipherBytes)
81+
{
82+
if (cipherBytes == null)
83+
{
84+
throw new ArgumentNullException(nameof(cipherBytes));
85+
}
86+
using var inputStream = new System.IO.MemoryStream(cipherBytes);
87+
using var outputStream = new System.IO.MemoryStream();
88+
streamCipher.DecryptAsync(inputStream, outputStream).GetAwaiter().GetResult();
89+
return outputStream.ToArray();
90+
}
5691
}
5792
}

0 commit comments

Comments
 (0)