Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concept of encrypting int values #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ public EncryptionConverter(IEncryptionProvider encryptionProvider, ConverterMapp
{
}
}

internal sealed class IntEncryptionConverter : ValueConverter<int, string>
{
public IntEncryptionConverter(IEncryptionProvider encryptionProvider, ConverterMappingHints mappingHints = null) : base(x => encryptionProvider.EncryptInt(x), x => encryptionProvider.DecryptInt(x), mappingHints)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static void UseEncryption(this ModelBuilder modelBuilder, IEncryptionProv
throw new ArgumentNullException(nameof(encryptionProvider), "You should create encryption provider.");

var encryptionConverter = new EncryptionConverter(encryptionProvider);
var intEncryptionConverter = new IntEncryptionConverter(encryptionProvider);
foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes())
{
foreach (IMutableProperty property in entityType.GetProperties())
Expand All @@ -29,6 +30,13 @@ public static void UseEncryption(this ModelBuilder modelBuilder, IEncryptionProv
if(attributes.Any())
property.SetValueConverter(encryptionConverter);
}

if((property.ClrType == typeof(int) || property.ClrType == typeof(int?)) && !IsDiscriminator(property))
{
object[] attributes = property.PropertyInfo.GetCustomAttributes(typeof(EncryptColumnAttribute), false);
if (attributes.Any())
property.SetValueConverter(intEncryptionConverter);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ public interface IEncryptionProvider
{
string Encrypt(string dataToEncrypt);
string Decrypt(string dataToDecrypt);

string EncryptInt(int dataToEncrypt);
int DecryptInt(string dataToDecrypt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,15 @@ public string Decrypt(string dataToDecrypt)
}
}
}

public string EncryptInt(int dataToEncrypt)
{
return Encrypt(dataToEncrypt.ToString());
}

public int DecryptInt(string dataToDecrypt)
{
return int.Parse(Decrypt(dataToDecrypt));
}
}
}