Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ using SafeCrypt.Models;

class Program
{
static void Main()
static async Task Main()
{
var aesEncryptor = new AesEncryption();

Expand All @@ -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}");
Expand All @@ -80,7 +80,7 @@ using SafeCrypt.Models;

class Program
{
static void Main()
static async Task Main()
{
var dataToEncrypt = "Data to Encrypt";

Expand All @@ -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);
Expand All @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions src/Encryption/AesEncryption/BaseAesEncryption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace SafeCrypt.AesEncryption
{
Expand All @@ -25,7 +26,7 @@ public class BaseAesEncryption
/// <exception cref="Exception">
/// Thrown for general encryption-related exceptions.
/// </exception>
internal static byte[] EncryptAES(ByteEncryptionParameters param)
internal static async Task<byte[]> EncryptAES(ByteEncryptionParameters param)
{
try
{
Expand All @@ -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
Expand Down Expand Up @@ -73,7 +74,7 @@ internal static byte[] EncryptAES(ByteEncryptionParameters param)
/// <exception cref="ArgumentNullException">
/// Thrown if the input encrypted data, key, or initialization vector is null.
/// </exception>
internal static byte[] DecryptAES(ByteDecryptionParameters param)
internal static async Task<byte[]> DecryptAES(ByteDecryptionParameters param)
{
try
{
Expand All @@ -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();
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/Encryption/AesEncryption/Decrypting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DecryptionData> DeEncryptFromHexString(DecryptionParameters param)
{
var responseData = new DecryptionData();

Expand Down Expand Up @@ -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
{
Expand All @@ -53,7 +54,7 @@ public DecryptionData DeEncryptFromHexString(DecryptionParameters param)
};
}

public DecryptionData DecryptFromBase64String(DecryptionParameters param)
public async Task<DecryptionData> DecryptFromBase64String(DecryptionParameters param)
{
var responseData = new DecryptionData();

Expand Down Expand Up @@ -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
{
Expand Down
9 changes: 5 additions & 4 deletions src/Encryption/AesEncryption/Encrypting.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using SafeCrypt.AesEncryption;
using SafeCrypt.Helpers;
using SafeCrypt.Models;
Expand All @@ -21,7 +22,7 @@ public class AesEncryption : BaseAesEncryption
/// <param name="secretKey">The secret key used for encryption.</param>
/// <param name="iv">The initialization vector used for encryption.</param>
/// <returns>The encrypted data as a byte array.</returns>
public EncryptionData EncryptToHexString(EncryptionParameters param)
public async Task<EncryptionData> EncryptToHexString(EncryptionParameters param)
{
var responseData = new EncryptionData();

Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -90,7 +91,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param)
/// <exception cref="FormatException">
/// Thrown if the base64secretKey is not a valid Base64-encoded string.
/// </exception>
public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string base64secretKey)
public async Task<EncryptionData> EncryptToBase64String(string dataToBeEncrypted, string base64secretKey)
{
// validate is base64
if (!Validators.IsBase64String(base64secretKey))
Expand All @@ -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
{
Expand Down