From 374a754ed6fd0b814a07d7e51dce10007a443208 Mon Sep 17 00:00:00 2001 From: Oluwasegun Akinpelu Date: Mon, 8 Jan 2024 13:39:10 +0100 Subject: [PATCH] Implemented Asynchroonous methods and also updated the readme to account for the implementation --- README.md | 10 +++++----- src/Encryption/AesEncryption/BaseAesEncryption.cs | 9 +++++---- src/Encryption/AesEncryption/Decrypting.cs | 9 +++++---- src/Encryption/AesEncryption/Encrypting.cs | 9 +++++---- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 083efc5..72329ec 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ using SafeCrypt.Models; class Program { - static void Main() + static async Task Main() { var aesEncryptor = new AesEncryption(); @@ -64,7 +64,7 @@ class Program }; - var data = aesDecryptor.DecryptFromBase64String(parameterToDecrypt) + var data = await aesDecryptor.DecryptFromBase64String(parameterToDecrypt) Console.WriteLine($"Decrypted Data: {data.DecryptedData}"); Console.WriteLine($"Initialization Vector: {data.Iv}"); @@ -80,7 +80,7 @@ using SafeCrypt.Models; class Program { - static void Main() + static async Task Main() { var dataToEncrypt = "Data to Encrypt"; @@ -96,7 +96,7 @@ class Program var encryptor = new AesEncryption(); - var response = encryptor.EncryptToBase64String(encryptionParam.DataToEncrypt, secret); + var response = await encryptor.EncryptToBase64String(encryptionParam.DataToEncrypt, secret); Console.WriteLine(response.EncryptedData); Console.WriteLine(response.Iv); @@ -113,7 +113,7 @@ class Program var decryptor = new AesDecryption(); - var decryptionData = decryptor.DecryptFromBase64String(decryptorParam); + var decryptionData = await decryptor.DecryptFromBase64String(decryptorParam); Console.WriteLine(decryptionData.DecryptedData); Console.WriteLine(decryptionData.Iv); diff --git a/src/Encryption/AesEncryption/BaseAesEncryption.cs b/src/Encryption/AesEncryption/BaseAesEncryption.cs index 9a35ca9..897ef71 100644 --- a/src/Encryption/AesEncryption/BaseAesEncryption.cs +++ b/src/Encryption/AesEncryption/BaseAesEncryption.cs @@ -4,6 +4,7 @@ using System.Reflection; using System.Security.Cryptography; using System.Text; +using System.Threading.Tasks; namespace SafeCrypt.AesEncryption { @@ -25,7 +26,7 @@ public class BaseAesEncryption /// /// Thrown for general encryption-related exceptions. /// - internal static byte[] EncryptAES(ByteEncryptionParameters param) + internal static async Task EncryptAES(ByteEncryptionParameters param) { try { @@ -45,7 +46,7 @@ internal static byte[] EncryptAES(ByteEncryptionParameters param) using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { // Write the data to be encrypted to the CryptoStream - cryptoStream.Write(param.Data, 0, param.Data.Length); + await cryptoStream.WriteAsync(param.Data, 0, param.Data.Length); cryptoStream.FlushFinalBlock(); // Return the encrypted data as a byte array @@ -73,7 +74,7 @@ internal static byte[] EncryptAES(ByteEncryptionParameters param) /// /// Thrown if the input encrypted data, key, or initialization vector is null. /// - internal static byte[] DecryptAES(ByteDecryptionParameters param) + internal static async Task DecryptAES(ByteDecryptionParameters param) { try { @@ -97,7 +98,7 @@ internal static byte[] DecryptAES(ByteDecryptionParameters param) using (MemoryStream decryptedStream = new MemoryStream()) { // Copy the decrypted data from the CryptoStream to the MemoryStream - cryptoStream.CopyTo(decryptedStream); + await cryptoStream.CopyToAsync(decryptedStream); return decryptedStream.ToArray(); } } diff --git a/src/Encryption/AesEncryption/Decrypting.cs b/src/Encryption/AesEncryption/Decrypting.cs index 4fe642b..2a20390 100644 --- a/src/Encryption/AesEncryption/Decrypting.cs +++ b/src/Encryption/AesEncryption/Decrypting.cs @@ -2,12 +2,13 @@ using SafeCrypt.Helpers; using SafeCrypt.Models; using System; +using System.Threading.Tasks; namespace SafeCrypt.AESDecryption { public class AesDecryption : BaseAesEncryption { - public DecryptionData DeEncryptFromHexString(DecryptionParameters param) + public async Task DeEncryptFromHexString(DecryptionParameters param) { var responseData = new DecryptionData(); @@ -43,7 +44,7 @@ public DecryptionData DeEncryptFromHexString(DecryptionParameters param) Data = param.DataToDecrypt.HexadecimalStringToByteArray() }; - var response = DecryptAES(byteEncryptionParameters); + var response = await DecryptAES(byteEncryptionParameters); return new DecryptionData { @@ -53,7 +54,7 @@ public DecryptionData DeEncryptFromHexString(DecryptionParameters param) }; } - public DecryptionData DecryptFromBase64String(DecryptionParameters param) + public async Task DecryptFromBase64String(DecryptionParameters param) { var responseData = new DecryptionData(); @@ -81,7 +82,7 @@ public DecryptionData DecryptFromBase64String(DecryptionParameters param) Data = Convert.FromBase64String(param.DataToDecrypt) }; - var response = DecryptAES(byteDecryptionParameters); + var response = await DecryptAES(byteDecryptionParameters); return new DecryptionData { diff --git a/src/Encryption/AesEncryption/Encrypting.cs b/src/Encryption/AesEncryption/Encrypting.cs index 3f94479..39efa66 100644 --- a/src/Encryption/AesEncryption/Encrypting.cs +++ b/src/Encryption/AesEncryption/Encrypting.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using SafeCrypt.AesEncryption; using SafeCrypt.Helpers; using SafeCrypt.Models; @@ -21,7 +22,7 @@ public class AesEncryption : BaseAesEncryption /// The secret key used for encryption. /// The initialization vector used for encryption. /// The encrypted data as a byte array. - public EncryptionData EncryptToHexString(EncryptionParameters param) + public async Task EncryptToHexString(EncryptionParameters param) { var responseData = new EncryptionData(); @@ -57,7 +58,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param) Data = param.DataToEncrypt.ConvertToHexString().HexadecimalStringToByteArray() }; - var response = EncryptAES(byteEncryptionParameters); + var response = await EncryptAES(byteEncryptionParameters); return new EncryptionData { @@ -90,7 +91,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param) /// /// Thrown if the base64secretKey is not a valid Base64-encoded string. /// - public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string base64secretKey) + public async Task EncryptToBase64String(string dataToBeEncrypted, string base64secretKey) { // validate is base64 if (!Validators.IsBase64String(base64secretKey)) @@ -110,7 +111,7 @@ public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string bas Data = dataToBeEncrypted.ConvertToHexString().HexadecimalStringToByteArray() }; - var response = EncryptAES(byteEncryptionParameters); + var response = await EncryptAES(byteEncryptionParameters); return new EncryptionData {