Skip to content

Commit 6b7d2d2

Browse files
authored
Merge pull request #37 from oluwasege/feat/async-support-implementation
feat: Implemented Asynchronous methods and also updated the readme to accommodate new implementations.
2 parents 71a61f5 + 374a754 commit 6b7d2d2

4 files changed

Lines changed: 20 additions & 17 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ using SafeCrypt.Models;
4545

4646
class Program
4747
{
48-
static void Main()
48+
static async Task Main()
4949
{
5050
var aesEncryptor = new AesEncryption();
5151

@@ -64,7 +64,7 @@ class Program
6464

6565
};
6666

67-
var data = aesDecryptor.DecryptFromBase64String(parameterToDecrypt)
67+
var data = await aesDecryptor.DecryptFromBase64String(parameterToDecrypt)
6868

6969
Console.WriteLine($"Decrypted Data: {data.DecryptedData}");
7070
Console.WriteLine($"Initialization Vector: {data.Iv}");
@@ -80,7 +80,7 @@ using SafeCrypt.Models;
8080

8181
class Program
8282
{
83-
static void Main()
83+
static async Task Main()
8484
{
8585
var dataToEncrypt = "Data to Encrypt";
8686

@@ -96,7 +96,7 @@ class Program
9696

9797
var encryptor = new AesEncryption();
9898

99-
var response = encryptor.EncryptToBase64String(encryptionParam.DataToEncrypt, secret);
99+
var response = await encryptor.EncryptToBase64String(encryptionParam.DataToEncrypt, secret);
100100

101101
Console.WriteLine(response.EncryptedData);
102102
Console.WriteLine(response.Iv);
@@ -113,7 +113,7 @@ class Program
113113

114114

115115
var decryptor = new AesDecryption();
116-
var decryptionData = decryptor.DecryptFromBase64String(decryptorParam);
116+
var decryptionData = await decryptor.DecryptFromBase64String(decryptorParam);
117117

118118
Console.WriteLine(decryptionData.DecryptedData);
119119
Console.WriteLine(decryptionData.Iv);

src/Encryption/AesEncryption/BaseAesEncryption.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reflection;
55
using System.Security.Cryptography;
66
using System.Text;
7+
using System.Threading.Tasks;
78

89
namespace SafeCrypt.AesEncryption
910
{
@@ -25,7 +26,7 @@ public class BaseAesEncryption
2526
/// <exception cref="Exception">
2627
/// Thrown for general encryption-related exceptions.
2728
/// </exception>
28-
internal static byte[] EncryptAES(ByteEncryptionParameters param)
29+
internal static async Task<byte[]> EncryptAES(ByteEncryptionParameters param)
2930
{
3031
try
3132
{
@@ -45,7 +46,7 @@ internal static byte[] EncryptAES(ByteEncryptionParameters param)
4546
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
4647
{
4748
// Write the data to be encrypted to the CryptoStream
48-
cryptoStream.Write(param.Data, 0, param.Data.Length);
49+
await cryptoStream.WriteAsync(param.Data, 0, param.Data.Length);
4950
cryptoStream.FlushFinalBlock();
5051

5152
// Return the encrypted data as a byte array
@@ -73,7 +74,7 @@ internal static byte[] EncryptAES(ByteEncryptionParameters param)
7374
/// <exception cref="ArgumentNullException">
7475
/// Thrown if the input encrypted data, key, or initialization vector is null.
7576
/// </exception>
76-
internal static byte[] DecryptAES(ByteDecryptionParameters param)
77+
internal static async Task<byte[]> DecryptAES(ByteDecryptionParameters param)
7778
{
7879
try
7980
{
@@ -97,7 +98,7 @@ internal static byte[] DecryptAES(ByteDecryptionParameters param)
9798
using (MemoryStream decryptedStream = new MemoryStream())
9899
{
99100
// Copy the decrypted data from the CryptoStream to the MemoryStream
100-
cryptoStream.CopyTo(decryptedStream);
101+
await cryptoStream.CopyToAsync(decryptedStream);
101102
return decryptedStream.ToArray();
102103
}
103104
}

src/Encryption/AesEncryption/Decrypting.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
using SafeCrypt.Helpers;
33
using SafeCrypt.Models;
44
using System;
5+
using System.Threading.Tasks;
56

67
namespace SafeCrypt.AESDecryption
78
{
89
public class AesDecryption : BaseAesEncryption
910
{
10-
public DecryptionData DeEncryptFromHexString(DecryptionParameters param)
11+
public async Task<DecryptionData> DeEncryptFromHexString(DecryptionParameters param)
1112
{
1213
var responseData = new DecryptionData();
1314

@@ -43,7 +44,7 @@ public DecryptionData DeEncryptFromHexString(DecryptionParameters param)
4344
Data = param.DataToDecrypt.HexadecimalStringToByteArray()
4445
};
4546

46-
var response = DecryptAES(byteEncryptionParameters);
47+
var response = await DecryptAES(byteEncryptionParameters);
4748

4849
return new DecryptionData
4950
{
@@ -53,7 +54,7 @@ public DecryptionData DeEncryptFromHexString(DecryptionParameters param)
5354
};
5455
}
5556

56-
public DecryptionData DecryptFromBase64String(DecryptionParameters param)
57+
public async Task<DecryptionData> DecryptFromBase64String(DecryptionParameters param)
5758
{
5859
var responseData = new DecryptionData();
5960

@@ -81,7 +82,7 @@ public DecryptionData DecryptFromBase64String(DecryptionParameters param)
8182
Data = Convert.FromBase64String(param.DataToDecrypt)
8283
};
8384

84-
var response = DecryptAES(byteDecryptionParameters);
85+
var response = await DecryptAES(byteDecryptionParameters);
8586

8687
return new DecryptionData
8788
{

src/Encryption/AesEncryption/Encrypting.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23
using SafeCrypt.AesEncryption;
34
using SafeCrypt.Helpers;
45
using SafeCrypt.Models;
@@ -21,7 +22,7 @@ public class AesEncryption : BaseAesEncryption
2122
/// <param name="secretKey">The secret key used for encryption.</param>
2223
/// <param name="iv">The initialization vector used for encryption.</param>
2324
/// <returns>The encrypted data as a byte array.</returns>
24-
public EncryptionData EncryptToHexString(EncryptionParameters param)
25+
public async Task<EncryptionData> EncryptToHexString(EncryptionParameters param)
2526
{
2627
var responseData = new EncryptionData();
2728

@@ -57,7 +58,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param)
5758
Data = param.DataToEncrypt.ConvertToHexString().HexadecimalStringToByteArray()
5859
};
5960

60-
var response = EncryptAES(byteEncryptionParameters);
61+
var response = await EncryptAES(byteEncryptionParameters);
6162

6263
return new EncryptionData
6364
{
@@ -90,7 +91,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param)
9091
/// <exception cref="FormatException">
9192
/// Thrown if the base64secretKey is not a valid Base64-encoded string.
9293
/// </exception>
93-
public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string base64secretKey)
94+
public async Task<EncryptionData> EncryptToBase64String(string dataToBeEncrypted, string base64secretKey)
9495
{
9596
// validate is base64
9697
if (!Validators.IsBase64String(base64secretKey))
@@ -110,7 +111,7 @@ public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string bas
110111
Data = dataToBeEncrypted.ConvertToHexString().HexadecimalStringToByteArray()
111112
};
112113

113-
var response = EncryptAES(byteEncryptionParameters);
114+
var response = await EncryptAES(byteEncryptionParameters);
114115

115116
return new EncryptionData
116117
{

0 commit comments

Comments
 (0)