Skip to content

Commit 8663485

Browse files
authored
Merge pull request #53 from selfmadecode/52-use-a-generic-class-for-model-validation-in-validator-class
feat: add and use a generic validation class to validate encryption and decryption models
2 parents de80fe6 + 4e5991f commit 8663485

13 files changed

Lines changed: 156 additions & 220 deletions

File tree

src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static async Task<DecryptionData> DecryptFromHexStringAsync(DecryptionPar
2323
{
2424
var responseData = new DecryptionData();
2525

26-
Validators.ValidateNotNull(param);
26+
Validator<DecryptionParameters>.ValidateNotNull(param);
2727

2828
// validate is base64
2929
if (!Validators.IsBase64String(param.SecretKey))
@@ -52,7 +52,7 @@ public static async Task<DecryptionData> DecryptFromHexStringAsync(DecryptionPar
5252
{
5353
SecretKey = Convert.FromBase64String(param.SecretKey),
5454
IV = dataBytes,
55-
Data = param.DataToDecrypt.HexadecimalStringToByteArray()
55+
Data = param.Data.HexadecimalStringToByteArray()
5656
};
5757

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

82-
Validators.ValidateNotNull(param);
82+
Validator<DecryptionParameters>.ValidateNotNull(param);
8383

8484

8585
if (!Validators.IsBase64String(param.SecretKey))
@@ -100,7 +100,7 @@ public static async Task<DecryptionData> DecryptFromBase64StringAsync(Decryption
100100
{
101101
SecretKey = Convert.FromBase64String(param.SecretKey),
102102
IV = Convert.FromBase64String(param.IV),
103-
Data = Convert.FromBase64String(param.DataToDecrypt)
103+
Data = Convert.FromBase64String(param.Data)
104104
};
105105

106106
var response = await BaseAesEncryption.DecryptAsync(byteDecryptionParameters, mode);

src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static async Task<EncryptionData> EncryptToHexStringAsync(EncryptionParam
4949
{
5050
SecretKey = Convert.FromBase64String(param.SecretKey),
5151
IV = dataBytes,
52-
Data = param.DataToEncrypt.ConvertToHexString().HexadecimalStringToByteArray()
52+
Data = param.Data.ConvertToHexString().HexadecimalStringToByteArray()
5353
};
5454

5555
var response = await BaseAesEncryption.EncryptAsync(byteEncryptionParameters, mode);
@@ -118,7 +118,7 @@ private static EncryptionData ValidateEncryptionParameters(EncryptionParameters
118118
{
119119
var responseData = new EncryptionData();
120120

121-
Validators.ValidateNotNull(param);
121+
Validator<EncryptionParameters>.ValidateNotNull(param);
122122

123123
// validate is base64
124124
if (!Validators.IsBase64String(param.SecretKey))
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Collections.Generic;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace SafeCrypt.Models
5+
{
6+
public class BaseAesEncryptionParameters
7+
{
8+
/// <summary>
9+
/// Gets or sets the data to be encrypted.
10+
/// </summary>
11+
[Required]
12+
public string Data { get; set; }
13+
14+
/// <summary>
15+
/// Gets or sets the secret key used for encryption.
16+
/// </summary>
17+
[Required]
18+
public string SecretKey { get; set; }
19+
20+
/// <summary>
21+
/// Gets or sets the initialization vector (IV) used for encryption.
22+
/// </summary>
23+
[Required]
24+
public string IV { get; set; }
25+
}
26+
27+
public class BaseAesData
28+
{
29+
/// <summary>
30+
/// Gets or sets the initialization vector (IV) used for encryption.
31+
/// </summary>
32+
public string Iv { get; set; }
33+
34+
/// <summary>
35+
/// Gets or sets the secret key used for encryption. Should be a base64 string
36+
/// </summary>
37+
public string SecretKey { get; set; }
38+
39+
public bool HasError { get; set; }
40+
41+
public List<string> Errors { get; set; } = new List<string>();
42+
}
43+
}

src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Decrypt/DecryptionData.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,11 @@
22

33
namespace SafeCrypt.Models
44
{
5-
public class DecryptionData
5+
public class DecryptionData : BaseAesData
66
{
77
/// <summary>
8-
/// Gets or sets the data to be encrypted.
8+
/// Gets or sets the data to be decrypted.
99
/// </summary>
1010
public string DecryptedData { get; set; }
11-
12-
/// <summary>
13-
/// Gets or sets the initialization vector (IV) used for encryption.
14-
/// </summary>
15-
public string Iv { get; set; }
16-
17-
/// <summary>
18-
/// Gets or sets the secret key used for encryption. Should be a base64 string
19-
/// </summary>
20-
public string SecretKey { get; set; }
21-
22-
public bool HasError { get; set; }
23-
public List<string> Errors { get; set; } = new List<string>();
2411
}
2512
}
Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.ComponentModel.DataAnnotations;
4-
using System.Text;
5-
6-
namespace SafeCrypt.Models
1+
namespace SafeCrypt.Models
72
{
8-
public class DecryptionParameters
3+
public class DecryptionParameters : BaseAesEncryptionParameters
94
{
10-
/// <summary>
11-
/// Gets or sets the data to be decrypted.
12-
/// </summary>
13-
[Required]
14-
public string DataToDecrypt { get; set; }
15-
16-
/// <summary>
17-
/// Gets or sets the secret key used for decryption.
18-
/// </summary>
19-
[Required]
20-
public string SecretKey { get; set; }
21-
22-
/// <summary>
23-
/// Gets or sets the initialization vector (IV) used for decryption.
24-
/// </summary>
25-
[Required]
26-
public string IV { get; set; }
275
}
286
}

src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,11 @@
22

33
namespace SafeCrypt.Models
44
{
5-
/// <summary>
6-
/// Represents the data and initialization vector (IV) used in encryption.
7-
/// </summary>
8-
public class EncryptionData
5+
public class EncryptionData : BaseAesData
96
{
107
/// <summary>
118
/// Gets or sets the data to be encrypted.
129
/// </summary>
1310
public string EncryptedData { get; set; }
14-
15-
/// <summary>
16-
/// Gets or sets the initialization vector (IV) used for encryption.
17-
/// </summary>
18-
public string Iv { get; set; }
19-
20-
/// <summary>
21-
/// Gets or sets the secret key used for encryption. Should be a base64 string
22-
/// </summary>
23-
public string SecretKey { get; set; }
24-
25-
public bool HasError { get; set; }
26-
27-
public List<string> Errors { get; set; } = new List<string>();
2811
}
2912
}
Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,10 @@
1-
using System.ComponentModel.DataAnnotations;
2-
3-
4-
namespace SafeCrypt.Models
1+
namespace SafeCrypt.Models
52
{
6-
public class EncryptionParameters
3+
public class EncryptionParameters : BaseAesEncryptionParameters
74
{
8-
/// <summary>
9-
/// Gets or sets the data to be encrypted.
10-
/// </summary>
11-
[Required]
12-
public string DataToEncrypt { get; set; }
13-
14-
/// <summary>
15-
/// Gets or sets the secret key used for encryption.
16-
/// </summary>
17-
[Required]
18-
public string SecretKey { get; set; }
19-
20-
/// <summary>
21-
/// Gets or sets the initialization vector (IV) used for encryption.
22-
/// </summary>
23-
[Required]
24-
public string IV { get; set; }
255
}
266

27-
public class StringEncryptionParameters
7+
public class StringEncryptionParameters : BaseAesEncryptionParameters
288
{
29-
/// <summary>
30-
/// Gets or sets the data to be encrypted.
31-
/// </summary>
32-
[Required]
33-
public string Data { get; set; }
34-
35-
/// <summary>
36-
/// Gets or sets the secret key used for encryption.
37-
/// </summary>
38-
[Required]
39-
public string SecretKey { get; set; }
40-
41-
/// <summary>
42-
/// Gets or sets the initialization vector (IV) used for encryption.
43-
/// </summary>
44-
[Required]
45-
public string IV { get; set; }
469
}
47-
4810
}

src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using System.Collections.Generic;
42

53
namespace SafeCrypt.RsaEncryption.Models
64
{

src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ internal static async Task<RsaDecryptionResult> DecryptAsync(byte[] encryptedDat
7676
});
7777

7878
result.DecryptedData = decryptedData;
79-
8079
}
8180
catch (Exception ex)
8281
{

src/SafeCrypt.Lib/Helpers/Validators.cs

Lines changed: 35 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,83 +4,7 @@
44
namespace SafeCrypt.Helpers
55
{
66
internal static class Validators
7-
{
8-
/// <summary>
9-
/// Validates that the specified ByteEncryptionParameters instance is not null
10-
/// and that its required properties (Data, SecretKey, IV) are not null.
11-
/// </summary>
12-
/// <param name="parameters">The ByteEncryptionParameters instance to validate.</param>
13-
/// <exception cref="ArgumentNullException">
14-
/// Thrown if the specified parameters instance is null or if any of its required properties are null.
15-
/// </exception>
16-
internal static void ValidateNotNull(EncryptionParameters parameters)
17-
{
18-
if (parameters == null)
19-
{
20-
throw new ArgumentNullException(nameof(parameters), "ByteEncryptionParameters instance cannot be null.");
21-
}
22-
23-
if (parameters.DataToEncrypt == null)
24-
{
25-
throw new ArgumentNullException(nameof(parameters.DataToEncrypt), "DataToEncrypt property cannot be null.");
26-
}
27-
28-
if (parameters.SecretKey == null)
29-
{
30-
throw new ArgumentNullException(nameof(parameters.SecretKey), "SecretKey property cannot be null.");
31-
}
32-
33-
if (parameters.IV == null)
34-
{
35-
throw new ArgumentNullException(nameof(parameters.IV), "IV property cannot be null.");
36-
}
37-
}
38-
internal static void ValidateNotNull(DecryptionParameters parameters)
39-
{
40-
if (parameters == null)
41-
{
42-
throw new ArgumentNullException(nameof(parameters), "ByteEncryptionParameters instance cannot be null.");
43-
}
44-
45-
if (parameters.DataToDecrypt == null)
46-
{
47-
throw new ArgumentNullException(nameof(parameters.DataToDecrypt), "DataToDecrypt property cannot be null.");
48-
}
49-
50-
if (parameters.SecretKey == null)
51-
{
52-
throw new ArgumentNullException(nameof(parameters.SecretKey), "SecretKey property cannot be null.");
53-
}
54-
55-
if (parameters.IV == null)
56-
{
57-
throw new ArgumentNullException(nameof(parameters.IV), "IV property cannot be null.");
58-
}
59-
}
60-
61-
internal static void ValidateNotNull(StringEncryptionParameters parameters)
62-
{
63-
if (parameters == null)
64-
{
65-
throw new ArgumentNullException(nameof(parameters), "StringEncryptionParameters instance cannot be null.");
66-
}
67-
68-
if (parameters.Data == null)
69-
{
70-
throw new ArgumentNullException(nameof(parameters.Data), "Data property cannot be null.");
71-
}
72-
73-
if (parameters.SecretKey == null)
74-
{
75-
throw new ArgumentNullException(nameof(parameters.SecretKey), "SecretKey property cannot be null.");
76-
}
77-
78-
if (parameters.IV == null)
79-
{
80-
throw new ArgumentNullException(nameof(parameters.IV), "IV property cannot be null.");
81-
}
82-
}
83-
7+
{
848
/// <summary>
859
/// Validates whether the provided string is a valid Base64-encoded key.
8610
/// </summary>
@@ -128,4 +52,38 @@ private static bool IsValidByteArray(byte[] byteArray)
12852
return byteArray != null && byteArray.Length > 0;
12953
}
13054
}
55+
56+
internal static class Validator<TParameters> where TParameters : BaseAesEncryptionParameters
57+
{
58+
/// <summary>
59+
/// Validates that the specified ByteEncryptionParameters instance is not null
60+
/// and that its required properties (Data, SecretKey, IV) are not null.
61+
/// </summary>
62+
/// <param name="parameters">The ByteEncryptionParameters instance to validate.</param>
63+
/// <exception cref="ArgumentNullException">
64+
/// Thrown if the specified parameters instance is null or if any of its required properties are null.
65+
/// </exception>
66+
internal static void ValidateNotNull(TParameters parameters)
67+
{
68+
if (parameters == null)
69+
{
70+
throw new ArgumentNullException(nameof(parameters), $"{typeof(TParameters).Name} instance cannot be null.");
71+
}
72+
73+
if (parameters.Data == null)
74+
{
75+
throw new ArgumentNullException(nameof(parameters.Data), "Data property cannot be null.");
76+
}
77+
78+
if (parameters.SecretKey == null)
79+
{
80+
throw new ArgumentNullException(nameof(parameters.SecretKey), "SecretKey property cannot be null.");
81+
}
82+
83+
if (parameters.IV == null)
84+
{
85+
throw new ArgumentNullException(nameof(parameters.IV), "IV property cannot be null.");
86+
}
87+
}
88+
}
13189
}

0 commit comments

Comments
 (0)