Skip to content

Commit 346925c

Browse files
committed
feat: use StringEncryptionParameters validation check
1 parent 8fe7eb7 commit 346925c

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

SafeCrypt.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
9+
</ItemGroup>
10+
711
</Project>

src/Encryption/AesEncryption/Encrypting.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Text;
33
using SafeCrypt.src.Helpers;
44
using SafeCrypt.src.Encryption.AesEncryption.Models;
5+
using System.ComponentModel.DataAnnotations;
56

67
namespace SafeCrypt.src.Encrypt.AesEncryption
78
{
@@ -23,14 +24,17 @@ public class Encrypting : BaseAesEncryption
2324
/// <returns>The encrypted data as a byte array.</returns>
2425
public byte[] Encrypt(ByteEncryptionParameters param)
2526
{
27+
Validators.ValidateNotNull(param);
28+
2629
// Delegate the encryption to the underlying AES encryption method
2730
return EncryptAES(param);
2831
}
2932

3033

3134
public byte[] Encrypt(StringEncryptionParameters param)
3235
{
33-
NullChecks(param.Data, param.SecretKey, param.IV);
36+
Validators.ValidateNotNull(param);
37+
3438

3539
var byteEncryptionParameters = new ByteEncryptionParameters
3640
{
@@ -87,6 +91,7 @@ public AesEncrypted Encrypt(string data, string secretKey)
8791

8892
return responseData;
8993
}
94+
9095
/// <summary>
9196
/// Encrypts the provided byte data using the Advanced Encryption Standard (AES) algorithm
9297
/// and returns the encrypted data as a hexadecimal string.
@@ -105,6 +110,8 @@ public AesEncrypted Encrypt(string data, string secretKey)
105110
/// </exception>
106111
public string EncryptByteToHexString(ByteEncryptionParameters param)
107112
{
113+
Validators.ValidateNotNull(param);
114+
108115
var cipherText = EncryptAES(param);
109116

110117
// Convert the encrypted data to a hexadecimal string
@@ -129,6 +136,8 @@ public string EncryptByteToHexString(ByteEncryptionParameters param)
129136
/// </exception>
130137
public string EncryptByteToBase64String(ByteEncryptionParameters param)
131138
{
139+
Validators.ValidateNotNull(param);
140+
132141
var cipherText = EncryptAES(param);
133142

134143
return Convert.ToBase64String(cipherText);
@@ -152,6 +161,8 @@ public string EncryptByteToBase64String(ByteEncryptionParameters param)
152161
/// </exception>
153162
public string EncryptByteToString(ByteEncryptionParameters param)
154163
{
164+
Validators.ValidateNotNull(param);
165+
155166
var cipherText = EncryptAES(param);
156167

157168
return cipherText.BytesToString();
@@ -163,16 +174,13 @@ public string EncryptByteToString(ByteEncryptionParameters param)
163174

164175
//}
165176

166-
private void NullChecks(string data, string secretKey, string iv = "")
177+
private void NullChecks(string data, string secretKey)
167178
{
168179
if (data == null || data.Length <= 0)
169180
throw new ArgumentNullException(nameof(data));
170181

171182
if (secretKey == null || secretKey.Length <= 0)
172183
throw new ArgumentNullException(nameof(secretKey));
173-
174-
if (iv == null || iv.Length <= 0)
175-
throw new ArgumentNullException(nameof(iv));
176184
}
177185
}
178186

0 commit comments

Comments
 (0)