using ServiceShared.Crypto;
using System.Text;
using System.Text.Json;
namespace ServiceShared.Models.Request
{
public class EncryptedRequest
{
public string descriptor { get; set; }
public string encrypted_content { get; set; }
public string hmac { get; set; }
///
/// Default constructor of EncryptedRequest
///
public EncryptedRequest()
{
}
///
/// Constructor for EncryptedRequest
///
/// descriptor of the content object
/// content object that should be encrypted
/// shared deriveKey that should be used in the encrption
public EncryptedRequest(string descriptor, object contentObject, byte[] deriveKey)
{
this.Encrypt(descriptor, contentObject, deriveKey);
}
///
/// Constructor for EncryptedRequest
///
/// Descriptor of the content object
/// content object that should be encrypted
/// shared deriveKey that should be used in the encrption
/// returns true if encryption was successfully
public bool Encrypt(string descriptor, object? contentObject, byte[] deriveKey)
{
bool result = false;
try
{
if(deriveKey != null && deriveKey.Length > 0 && !string.IsNullOrEmpty(descriptor))
{
this.descriptor = AES.Encrypt(descriptor, deriveKey);
if (contentObject != null)
{
string json = JsonSerializer.Serialize(contentObject, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
if (!string.IsNullOrEmpty(json))
{
this.encrypted_content = AES.Encrypt(json, deriveKey);
if (!string.IsNullOrEmpty(this.encrypted_content))
{
this.hmac = SHA512.HMAC(this.encrypted_content, deriveKey);
if(!string.IsNullOrEmpty(hmac))
{
result = true;
}
}
}
}
else
{
this.hmac = SHA512.HMAC(this.descriptor, deriveKey);
if (!string.IsNullOrEmpty(hmac))
{
result = true;
}
}
}
}
catch (Exception ex)
{
Log.Error(ex, "ServiceOutside.Models.Request.EncryptedRequest", "Encrypt");
}
return result;
}
///
/// Decrypts the encrypted content
///
/// Type of content object
/// shared deriveKey for the decryption
/// returns a object with defined type
public T? Decrypt(byte[] deriveKey)
{
object? result = null;
try
{
if (deriveKey != null && !string.IsNullOrEmpty(this.descriptor))
{
this.descriptor = AES.Decrypt(this.descriptor, deriveKey);
if (!string.IsNullOrEmpty(this.encrypted_content))
{
string json = AES.Decrypt(this.encrypted_content, deriveKey);
if (!string.IsNullOrEmpty(json))
{
result = JsonSerializer.Deserialize(json, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
}
}
}
}
catch (Exception ex)
{
Log.Error(ex, "ServiceOutside.Models.Request.EncryptedRequest", "Decrypt(byte[])");
}
return (T?)result;
}
///
/// Decrypts the encrypted content
///
/// shared deriveKey for the decryption
/// returns a object
public object Decrypt(byte[] deriveKey)
{
object result = null;
try
{
if (deriveKey != null && deriveKey.Length > 0 && !string.IsNullOrEmpty(this.descriptor))
{
this.descriptor = AES.Decrypt(this.descriptor, deriveKey);
if (!string.IsNullOrEmpty(this.encrypted_content))
{
string json = AES.Decrypt(this.encrypted_content, deriveKey);
if (!string.IsNullOrEmpty(json))
{
result = JsonSerializer.Deserialize