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
8 changes: 4 additions & 4 deletions src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static async Task<DecryptionData> DecryptFromHexStringAsync(DecryptionPar
{
var responseData = new DecryptionData();

Validators.ValidateNotNull(param);
Validator<DecryptionParameters>.ValidateNotNull(param);

// validate is base64
if (!Validators.IsBase64String(param.SecretKey))
Expand Down Expand Up @@ -52,7 +52,7 @@ public static async Task<DecryptionData> DecryptFromHexStringAsync(DecryptionPar
{
SecretKey = Convert.FromBase64String(param.SecretKey),
IV = dataBytes,
Data = param.DataToDecrypt.HexadecimalStringToByteArray()
Data = param.Data.HexadecimalStringToByteArray()
};

var response = await BaseAesEncryption.DecryptAsync(byteEncryptionParameters, mode);
Expand All @@ -79,7 +79,7 @@ public static async Task<DecryptionData> DecryptFromBase64StringAsync(Decryption
{
var responseData = new DecryptionData();

Validators.ValidateNotNull(param);
Validator<DecryptionParameters>.ValidateNotNull(param);


if (!Validators.IsBase64String(param.SecretKey))
Expand All @@ -100,7 +100,7 @@ public static async Task<DecryptionData> DecryptFromBase64StringAsync(Decryption
{
SecretKey = Convert.FromBase64String(param.SecretKey),
IV = Convert.FromBase64String(param.IV),
Data = Convert.FromBase64String(param.DataToDecrypt)
Data = Convert.FromBase64String(param.Data)
};

var response = await BaseAesEncryption.DecryptAsync(byteDecryptionParameters, mode);
Expand Down
4 changes: 2 additions & 2 deletions src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static async Task<EncryptionData> EncryptToHexStringAsync(EncryptionParam
{
SecretKey = Convert.FromBase64String(param.SecretKey),
IV = dataBytes,
Data = param.DataToEncrypt.ConvertToHexString().HexadecimalStringToByteArray()
Data = param.Data.ConvertToHexString().HexadecimalStringToByteArray()
};

var response = await BaseAesEncryption.EncryptAsync(byteEncryptionParameters, mode);
Expand Down Expand Up @@ -118,7 +118,7 @@ private static EncryptionData ValidateEncryptionParameters(EncryptionParameters
{
var responseData = new EncryptionData();

Validators.ValidateNotNull(param);
Validator<EncryptionParameters>.ValidateNotNull(param);

// validate is base64
if (!Validators.IsBase64String(param.SecretKey))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace SafeCrypt.Models
{
public class BaseAesEncryptionParameters
{
/// <summary>
/// Gets or sets the data to be encrypted.
/// </summary>
[Required]
public string Data { get; set; }

/// <summary>
/// Gets or sets the secret key used for encryption.
/// </summary>
[Required]
public string SecretKey { get; set; }

/// <summary>
/// Gets or sets the initialization vector (IV) used for encryption.
/// </summary>
[Required]
public string IV { get; set; }
}

public class BaseAesData
{
/// <summary>
/// Gets or sets the initialization vector (IV) used for encryption.
/// </summary>
public string Iv { get; set; }

/// <summary>
/// Gets or sets the secret key used for encryption. Should be a base64 string
/// </summary>
public string SecretKey { get; set; }

public bool HasError { get; set; }

public List<string> Errors { get; set; } = new List<string>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,11 @@

namespace SafeCrypt.Models
{
public class DecryptionData
public class DecryptionData : BaseAesData
{
/// <summary>
/// Gets or sets the data to be encrypted.
/// Gets or sets the data to be decrypted.
/// </summary>
public string DecryptedData { get; set; }

/// <summary>
/// Gets or sets the initialization vector (IV) used for encryption.
/// </summary>
public string Iv { get; set; }

/// <summary>
/// Gets or sets the secret key used for encryption. Should be a base64 string
/// </summary>
public string SecretKey { get; set; }

public bool HasError { get; set; }
public List<string> Errors { get; set; } = new List<string>();
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace SafeCrypt.Models
namespace SafeCrypt.Models
{
public class DecryptionParameters
public class DecryptionParameters : BaseAesEncryptionParameters
{
/// <summary>
/// Gets or sets the data to be decrypted.
/// </summary>
[Required]
public string DataToDecrypt { get; set; }

/// <summary>
/// Gets or sets the secret key used for decryption.
/// </summary>
[Required]
public string SecretKey { get; set; }

/// <summary>
/// Gets or sets the initialization vector (IV) used for decryption.
/// </summary>
[Required]
public string IV { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,11 @@

namespace SafeCrypt.Models
{
/// <summary>
/// Represents the data and initialization vector (IV) used in encryption.
/// </summary>
public class EncryptionData
public class EncryptionData : BaseAesData
{
/// <summary>
/// Gets or sets the data to be encrypted.
/// </summary>
public string EncryptedData { get; set; }

/// <summary>
/// Gets or sets the initialization vector (IV) used for encryption.
/// </summary>
public string Iv { get; set; }

/// <summary>
/// Gets or sets the secret key used for encryption. Should be a base64 string
/// </summary>
public string SecretKey { get; set; }

public bool HasError { get; set; }

public List<string> Errors { get; set; } = new List<string>();
}
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,10 @@
using System.ComponentModel.DataAnnotations;


namespace SafeCrypt.Models
namespace SafeCrypt.Models
{
public class EncryptionParameters
public class EncryptionParameters : BaseAesEncryptionParameters
{
/// <summary>
/// Gets or sets the data to be encrypted.
/// </summary>
[Required]
public string DataToEncrypt { get; set; }

/// <summary>
/// Gets or sets the secret key used for encryption.
/// </summary>
[Required]
public string SecretKey { get; set; }

/// <summary>
/// Gets or sets the initialization vector (IV) used for encryption.
/// </summary>
[Required]
public string IV { get; set; }
}

public class StringEncryptionParameters
public class StringEncryptionParameters : BaseAesEncryptionParameters
{
/// <summary>
/// Gets or sets the data to be encrypted.
/// </summary>
[Required]
public string Data { get; set; }

/// <summary>
/// Gets or sets the secret key used for encryption.
/// </summary>
[Required]
public string SecretKey { get; set; }

/// <summary>
/// Gets or sets the initialization vector (IV) used for encryption.
/// </summary>
[Required]
public string IV { get; set; }
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;

namespace SafeCrypt.RsaEncryption.Models
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ internal static async Task<RsaDecryptionResult> DecryptAsync(byte[] encryptedDat
});

result.DecryptedData = decryptedData;

}
catch (Exception ex)
{
Expand Down
112 changes: 35 additions & 77 deletions src/SafeCrypt.Lib/Helpers/Validators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,7 @@
namespace SafeCrypt.Helpers
{
internal static class Validators
{
/// <summary>
/// Validates that the specified ByteEncryptionParameters instance is not null
/// and that its required properties (Data, SecretKey, IV) are not null.
/// </summary>
/// <param name="parameters">The ByteEncryptionParameters instance to validate.</param>
/// <exception cref="ArgumentNullException">
/// Thrown if the specified parameters instance is null or if any of its required properties are null.
/// </exception>
internal static void ValidateNotNull(EncryptionParameters parameters)
{
if (parameters == null)
{
throw new ArgumentNullException(nameof(parameters), "ByteEncryptionParameters instance cannot be null.");
}

if (parameters.DataToEncrypt == null)
{
throw new ArgumentNullException(nameof(parameters.DataToEncrypt), "DataToEncrypt property cannot be null.");
}

if (parameters.SecretKey == null)
{
throw new ArgumentNullException(nameof(parameters.SecretKey), "SecretKey property cannot be null.");
}

if (parameters.IV == null)
{
throw new ArgumentNullException(nameof(parameters.IV), "IV property cannot be null.");
}
}
internal static void ValidateNotNull(DecryptionParameters parameters)
{
if (parameters == null)
{
throw new ArgumentNullException(nameof(parameters), "ByteEncryptionParameters instance cannot be null.");
}

if (parameters.DataToDecrypt == null)
{
throw new ArgumentNullException(nameof(parameters.DataToDecrypt), "DataToDecrypt property cannot be null.");
}

if (parameters.SecretKey == null)
{
throw new ArgumentNullException(nameof(parameters.SecretKey), "SecretKey property cannot be null.");
}

if (parameters.IV == null)
{
throw new ArgumentNullException(nameof(parameters.IV), "IV property cannot be null.");
}
}

internal static void ValidateNotNull(StringEncryptionParameters parameters)
{
if (parameters == null)
{
throw new ArgumentNullException(nameof(parameters), "StringEncryptionParameters instance cannot be null.");
}

if (parameters.Data == null)
{
throw new ArgumentNullException(nameof(parameters.Data), "Data property cannot be null.");
}

if (parameters.SecretKey == null)
{
throw new ArgumentNullException(nameof(parameters.SecretKey), "SecretKey property cannot be null.");
}

if (parameters.IV == null)
{
throw new ArgumentNullException(nameof(parameters.IV), "IV property cannot be null.");
}
}

{
/// <summary>
/// Validates whether the provided string is a valid Base64-encoded key.
/// </summary>
Expand Down Expand Up @@ -128,4 +52,38 @@ private static bool IsValidByteArray(byte[] byteArray)
return byteArray != null && byteArray.Length > 0;
}
}

internal static class Validator<TParameters> where TParameters : BaseAesEncryptionParameters
{
/// <summary>
/// Validates that the specified ByteEncryptionParameters instance is not null
/// and that its required properties (Data, SecretKey, IV) are not null.
/// </summary>
/// <param name="parameters">The ByteEncryptionParameters instance to validate.</param>
/// <exception cref="ArgumentNullException">
/// Thrown if the specified parameters instance is null or if any of its required properties are null.
/// </exception>
internal static void ValidateNotNull(TParameters parameters)
{
if (parameters == null)
{
throw new ArgumentNullException(nameof(parameters), $"{typeof(TParameters).Name} instance cannot be null.");
}

if (parameters.Data == null)
{
throw new ArgumentNullException(nameof(parameters.Data), "Data property cannot be null.");
}

if (parameters.SecretKey == null)
{
throw new ArgumentNullException(nameof(parameters.SecretKey), "SecretKey property cannot be null.");
}

if (parameters.IV == null)
{
throw new ArgumentNullException(nameof(parameters.IV), "IV property cannot be null.");
}
}
}
}
Loading