Skip to content

Commit 806b25b

Browse files
committed
feat: add ValidateEncryptionParameters method
1 parent 8eacc63 commit 806b25b

1 file changed

Lines changed: 25 additions & 13 deletions

File tree

src/Encryption/AesEncryption/Encrypting.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
2+
using System.Security.Cryptography;
23
using SafeCrypt.AesEncryption;
34
using SafeCrypt.Helpers;
45
using SafeCrypt.Models;
5-
using SafeCrypt.src.Encryption.AesEncryption.Models;
66

77
namespace SafeCrypt.AESEncryption
88
{
@@ -26,20 +26,13 @@ public EncryptionData EncryptToHexString(EncryptionParameters param)
2626
{
2727
var responseData = new EncryptionData();
2828

29-
Validators.ValidateNotNull(param);
29+
var parameterValidation = ValidateEncryptionParameters(param);
3030

31-
// validate is base64
32-
if (!Validators.IsBase64String(param.SecretKey))
31+
if (parameterValidation.HasError)
3332
{
34-
AddError(responseData, $"SecretKey: {param.SecretKey} is not a base64 string");
35-
return responseData;
33+
return parameterValidation;
3634
}
3735

38-
if (!Validators.IsBase64String(param.IV))
39-
{
40-
AddError(responseData, $"IV: {param.IV} is not a base64 string");
41-
return responseData;
42-
}
4336
// Convert input string to bytes
4437
byte[] dataBytes = param.IV.ConvertKeysToBytes();
4538

@@ -67,8 +60,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param)
6760
SecretKey = param.SecretKey
6861
};
6962
}
70-
71-
63+
7264
/// <summary>
7365
/// Encrypts the provided string data using the Advanced Encryption Standard (AES) algorithm.
7466
/// </summary>
@@ -121,6 +113,26 @@ public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string bas
121113
};
122114
}
123115

116+
private EncryptionData ValidateEncryptionParameters(EncryptionParameters param)
117+
{
118+
var responseData = new EncryptionData();
119+
120+
Validators.ValidateNotNull(param);
121+
122+
// validate is base64
123+
if (!Validators.IsBase64String(param.SecretKey))
124+
{
125+
AddError(responseData, $"SecretKey: {param.SecretKey} is not a base64 string");
126+
}
127+
128+
if (!Validators.IsBase64String(param.IV))
129+
{
130+
AddError(responseData, $"IV: {param.IV} is not a base64 string");
131+
}
132+
133+
return responseData;
134+
}
135+
124136
private void NullChecks(string data, string secretKey)
125137
{
126138
if (data == null || data.Length <= 0)

0 commit comments

Comments
 (0)