Skip to content

Commit

Permalink
chore: streamlined Encryption service implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Russell Camo <[email protected]>
  • Loading branch information
russkyc committed Aug 26, 2023
1 parent 26135c9 commit f4894a8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 69 deletions.
88 changes: 25 additions & 63 deletions GroomWise.Infrastructure/Encryption/EncryptionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,45 +41,20 @@ public string Encrypt(string toEncrypt)
);
}

public T Encrypt<T>(T item)
{
// Get fields in item
item?.GetType()
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
// where field is type(String)
.Where(field => field.FieldType == typeof(string))
.ToList()
.ForEach(f =>
{
var currentValue = (string)f.GetValue(item)!;
f.SetValue(
item,
string.IsNullOrEmpty(currentValue)
? string.Empty
: EncryptProvider.AESEncrypt(
currentValue,
CredentialStore.Instance.Get("AesKey"),
CredentialStore.Instance.Get("AesIv")
)
);
});
return item;
}

public T Encrypt<T>(T item, params string[] ignore)
{
item?.GetType()
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Where(field => field.FieldType == typeof(string))
.ToList()
.ForEach(f =>
.ForEach(fieldInfo =>
{
var currentValue = (string)f.GetValue(item)!;
var currentValue = (string)fieldInfo.GetValue(item)!;
if (
!string.IsNullOrEmpty(currentValue)
&& !ignore.ToList().Any(fieldName => f.Name.Contains(fieldName))
&& !ignore.ToList().Any(fieldName => fieldInfo.Name.Contains(fieldName))
)
f.SetValue(
fieldInfo.SetValue(
item,
string.IsNullOrEmpty(currentValue)
? string.Empty
Expand All @@ -102,53 +77,40 @@ public string Decrypt(string toDecrypt)
);
}

public T Decrypt<T>(T item)
public T Decrypt<T>(T item, params string[] ignore)
{
// Get fields in item
item?.GetType()
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
// where field is type(String)
.Where(field => field.FieldType == typeof(string))
.ToList()
.ForEach(f =>
.ForEach(fieldInfo =>
{
var currentValue = (string)f.GetValue(item)!;
f.SetValue(
item,
string.IsNullOrEmpty(currentValue)
? string.Empty
: EncryptProvider.AESDecrypt(
currentValue,
CredentialStore.Instance.Get("AesKey"),
CredentialStore.Instance.Get("AesIv")
)
);
var currentValue = (string)fieldInfo.GetValue(item)!;
if (
!string.IsNullOrEmpty(currentValue)
&& !ignore.ToList().Any(fieldName => fieldInfo.Name.Contains(fieldName))
)
{
fieldInfo.SetValue(
item,
string.IsNullOrEmpty(currentValue)
? string.Empty
: EncryptProvider.AESDecrypt(
currentValue,
CredentialStore.Instance.Get("AesKey"),
CredentialStore.Instance.Get("AesIv")
)
);
}
});
return item;
}

public string Hash(string item)
{
return item.SHA256();
}

public T Hash<T>(T item)
public string Hash(string toDecrypt)
{
// Get fields in item
item?.GetType()
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
// where field is type(String)
.Where(field => field.FieldType == typeof(string))
.ToList()
.ForEach(f =>
{
var currentValue = (string)f.GetValue(item)!;
f.SetValue(
item,
string.IsNullOrEmpty(currentValue) ? string.Empty : currentValue.SHA256()
);
});
return item;
return toDecrypt.SHA256();
}

public T Hash<T>(T item, params string[] ignore)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023 Russell Camo (Russkyc).- All Rights Reserved
//
//
// Unauthorized copying or redistribution of all files, in source and binary forms via any medium
// without written, signed consent from the author is strictly prohibited.

Expand All @@ -8,11 +8,9 @@ namespace GroomWise.Infrastructure.Encryption.Interfaces;
public interface IEncryptionService
{
string Encrypt(string toEncrypt);
T Encrypt<T>(T item);
T Encrypt<T>(T item, params string[] ignore);
string Decrypt(string toDecrypt);
T Decrypt<T>(T item);
string Hash(string item);
T Hash<T>(T item);
T Decrypt<T>(T item, params string[] ignore);
string Hash(string toDecrypt);
T Hash<T>(T item, params string[] ignore);
}
}

0 comments on commit f4894a8

Please sign in to comment.