Skip to content

Commit 64fa175

Browse files
author
Filipe GOMES PEIXOTO
committed
Add AesSample
1 parent 9d3c934 commit 64fa175

File tree

7 files changed

+129
-4
lines changed

7 files changed

+129
-4
lines changed

EntityFrameworkCore.DataEncryption.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "global", "global", "{3A8D80
1818
README.md = README.md
1919
EndProjectSection
2020
EndProject
21+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{64C3D7D1-67B8-4070-AE67-C71B761535CC}"
22+
EndProject
23+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AesSample", "samples\AesSample\AesSample.csproj", "{8AA1E576-4016-4623-96C8-90330F05F9A8}"
24+
EndProject
2125
Global
2226
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2327
Debug|Any CPU = Debug|Any CPU
@@ -32,13 +36,18 @@ Global
3236
{5E023B6A-0B47-4EC2-90B9-2DF998E58ADB}.Debug|Any CPU.Build.0 = Debug|Any CPU
3337
{5E023B6A-0B47-4EC2-90B9-2DF998E58ADB}.Release|Any CPU.ActiveCfg = Release|Any CPU
3438
{5E023B6A-0B47-4EC2-90B9-2DF998E58ADB}.Release|Any CPU.Build.0 = Release|Any CPU
39+
{8AA1E576-4016-4623-96C8-90330F05F9A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
40+
{8AA1E576-4016-4623-96C8-90330F05F9A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
41+
{8AA1E576-4016-4623-96C8-90330F05F9A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
42+
{8AA1E576-4016-4623-96C8-90330F05F9A8}.Release|Any CPU.Build.0 = Release|Any CPU
3543
EndGlobalSection
3644
GlobalSection(SolutionProperties) = preSolution
3745
HideSolutionNode = FALSE
3846
EndGlobalSection
3947
GlobalSection(NestedProjects) = preSolution
4048
{D037F8D0-E606-4C5A-8669-DB6AAE7B056B} = {3EC10767-1816-46B2-A78E-9856071CCFDB}
4149
{5E023B6A-0B47-4EC2-90B9-2DF998E58ADB} = {E4089551-AF4E-41B3-A6F8-2501A3BE0E0C}
50+
{8AA1E576-4016-4623-96C8-90330F05F9A8} = {64C3D7D1-67B8-4070-AE67-C71B761535CC}
4251
EndGlobalSection
4352
GlobalSection(ExtensibilityGlobals) = postSolution
4453
SolutionGuid = {4997BAE9-29BF-4D79-AE5E-5605E7A0F049}

samples/AesSample/AesSample.csproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.1" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.1" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\..\src\EntityFrameworkCore.DataEncryption\EntityFrameworkCore.DataEncryption.csproj" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.DataEncryption;
3+
4+
namespace AesSample
5+
{
6+
public class DatabaseContext : DbContext
7+
{
8+
private readonly IEncryptionProvider _encryptionProvider;
9+
10+
public DbSet<UserEntity> Users { get; set; }
11+
12+
public DatabaseContext(DbContextOptions<DatabaseContext> options, IEncryptionProvider encryptionProvider)
13+
: base(options)
14+
{
15+
_encryptionProvider = encryptionProvider;
16+
}
17+
18+
protected override void OnModelCreating(ModelBuilder modelBuilder)
19+
{
20+
modelBuilder.UseEncryption(_encryptionProvider);
21+
22+
base.OnModelCreating(modelBuilder);
23+
}
24+
}
25+
}

samples/AesSample/Program.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.DataEncryption.Providers;
3+
using System;
4+
using System.Linq;
5+
6+
namespace AesSample
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
var options = new DbContextOptionsBuilder<DatabaseContext>()
13+
.UseInMemoryDatabase(databaseName: "MyInMemoryDatabase")
14+
.Options;
15+
16+
// AES key randomly generated at each run.
17+
byte[] encryptionKey = AesProvider.GenerateKey(AesKeySize.AES256Bits).Key;
18+
var encryptionProvider = new AesProvider(encryptionKey);
19+
20+
using var context = new DatabaseContext(options, encryptionProvider);
21+
22+
var user = new UserEntity
23+
{
24+
FirstName = "John",
25+
LastName = "Doe",
26+
Email = "john@doe.com"
27+
};
28+
29+
context.Users.Add(user);
30+
context.SaveChanges();
31+
32+
Console.WriteLine($"Users count: {context.Users.Count()}");
33+
34+
user = context.Users.FirstOrDefault();
35+
36+
Console.WriteLine($"User: {user.FirstName} {user.LastName} - {user.Email}");
37+
}
38+
}
39+
}

samples/AesSample/UserEntity.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
using System.ComponentModel.DataAnnotations.Schema;
4+
5+
namespace AesSample
6+
{
7+
public class UserEntity
8+
{
9+
[Key]
10+
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
11+
public Guid Id { get; set; }
12+
13+
[Required]
14+
public string FirstName { get; set; }
15+
16+
[Required]
17+
public string LastName { get; set; }
18+
19+
[Required]
20+
[Encrypted]
21+
public string Email { get; set; }
22+
}
23+
}

src/EntityFrameworkCore.DataEncryption/ModelBuilderExtensions.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public static class ModelBuilderExtensions
1818
/// <param name="encryptionProvider">Encryption provider.</param>
1919
public static void UseEncryption(this ModelBuilder modelBuilder, IEncryptionProvider encryptionProvider)
2020
{
21+
if (modelBuilder is null)
22+
{
23+
throw new ArgumentNullException(nameof(modelBuilder), "The given model builder cannot be null");
24+
}
25+
2126
if (encryptionProvider is null)
2227
{
2328
throw new ArgumentNullException(nameof(encryptionProvider), "Cannot initialize encryption with a null provider.");
@@ -42,9 +47,12 @@ public static void UseEncryption(this ModelBuilder modelBuilder, IEncryptionProv
4247
}
4348
}
4449

45-
private static bool IsDiscriminator(IMutableProperty property)
46-
{
47-
return property.Name == "Discriminator" || property.PropertyInfo == null;
48-
}
50+
/// <summary>
51+
/// Gets a boolean value that indicates if the given property is a descrimitator.
52+
/// </summary>
53+
/// <param name="property"></param>
54+
/// <returns></returns>
55+
private static bool IsDiscriminator(IMutableProperty property)
56+
=> property.Name == "Discriminator" || property.PropertyInfo == null;
4957
}
5058
}

src/EntityFrameworkCore.DataEncryption/Providers/AesProvider.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ public string Decrypt(string dataToDecrypt)
109109
return decrypted;
110110
}
111111

112+
/// <summary>
113+
/// Generates an AES cryptography provider.
114+
/// </summary>
115+
/// <returns></returns>
112116
private AesCryptoServiceProvider CreateCryptographyProvider()
113117
{
114118
return new AesCryptoServiceProvider

0 commit comments

Comments
 (0)