Skip to content

Commit 2cd78ab

Browse files
committed
Update readme
Update sample versions
1 parent 6eb2c9e commit 2cd78ab

File tree

4 files changed

+72
-30
lines changed

4 files changed

+72
-30
lines changed

README.md

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,29 @@ Install the package from [NuGet](https://www.nuget.org/) or from the `Package Ma
2020
PM> Install-Package EntityFrameworkCore.DataEncryption
2121
```
2222

23-
## How to use
23+
## Supported types
2424

25-
To use `EntityFrameworkCore.DataEncryption`, you will need to decorate your `string` or `byte[]` properties of your entities with the `[Encrypted]` attribute and enable the encryption on the `ModelBuilder`.
25+
| Type | Default storage type |
26+
|------|----------------------|
27+
| `string` | Base64 string |
28+
| `byte[]` | BINARY |
2629

27-
To enable the encryption correctly, you will need to use an **encryption provider**, there is a list of the available providers:
30+
## Built-in providers
2831

2932
| Name | Class | Extra |
3033
|------|-------|-------|
3134
| [AES](https://learn.microsoft.com/en-US/dotnet/api/system.security.cryptography.aes?view=net-6.0) | [AesProvider](https://github.com/Eastrall/EntityFrameworkCore.DataEncryption/blob/main/src/EntityFrameworkCore.DataEncryption/Providers/AesProvider.cs) | Can use a 128bits, 192bits or 256bits key |
3235

33-
### Example with `AesProvider`
36+
## How to use
37+
38+
`EntityFrameworkCore.DataEncryption` supports 2 differents initialization methods:
39+
* Attribute
40+
* Fluent configuration
41+
42+
Depending on the initialization method you will use, you will need to decorate your `string` or `byte[]` properties of your entities with the `[Encrypted]` attribute or use the fluent `IsEncrypted()` method in your model configuration process.
43+
To use an encryption provider on your EF Core model, and enable the encryption on the `ModelBuilder`.
44+
45+
### Example with `AesProvider` and attribute
3446

3547
```csharp
3648
public class UserEntity
@@ -59,16 +71,55 @@ public class DatabaseContext : DbContext
5971
public DatabaseContext(DbContextOptions options)
6072
: base(options)
6173
{
62-
this._provider = new AesProvider(this._encryptionKey, this._encryptionIV);
74+
_provider = new AesProvider(this._encryptionKey, this._encryptionIV);
6375
}
6476

6577
protected override void OnModelCreating(ModelBuilder modelBuilder)
6678
{
67-
modelBuilder.UseEncryption(this._provider);
79+
modelBuilder.UseEncryption(_provider);
80+
}
81+
}
82+
```
83+
The code bellow creates a new [`AesProvider`](https://github.com/Eastrall/EntityFrameworkCore.DataEncryption/blob/main/src/EntityFrameworkCore.DataEncryption/Providers/AesProvider.cs) and gives it to the current model. It will encrypt every `string` fields of your model that has the `[Encrypted]` attribute when saving changes to database. As for the decrypt process, it will be done when reading the `DbSet<T>` of your `DbContext`.
84+
85+
### Example with `AesProvider` and fluent configuration
86+
87+
```csharp
88+
public class UserEntity
89+
{
90+
public int Id { get; set; }
91+
public string Username { get; set; }
92+
public string Password { get; set; }
93+
public int Age { get; set; }
94+
}
95+
96+
public class DatabaseContext : DbContext
97+
{
98+
// Get key and IV from a Base64String or any other ways.
99+
// You can generate a key and IV using "AesProvider.GenerateKey()"
100+
private readonly byte[] _encryptionKey = ...;
101+
private readonly byte[] _encryptionIV = ...;
102+
private readonly IEncryptionProvider _provider;
103+
104+
public DbSet<UserEntity> Users { get; set; }
105+
106+
public DatabaseContext(DbContextOptions options)
107+
: base(options)
108+
{
109+
_provider = new AesProvider(this._encryptionKey, this._encryptionIV);
110+
}
111+
112+
protected override void OnModelCreating(ModelBuilder modelBuilder)
113+
{
114+
var userEntityBuilder = modelBuilder.Entity<UserEntity>();
115+
116+
userEntityBuilder.Property(x => x.Username).IsRequired().IsEncrypted();
117+
userEntityBuilder.Property(x => x.Password).IsRequired().IsEncrypted();
118+
119+
modelBuilder.UseEncryption(_provider);
68120
}
69121
}
70122
```
71-
The code bellow creates a new `AesEncryption` provider and gives it to the current model. It will encrypt every `string` fields of your model that has the `[Encrypted]` attribute when saving changes to database. As for the decrypt process, it will be done when reading the `DbSet<T>` of your `DbContext`.
72123

73124
## Create an encryption provider
74125

@@ -98,12 +149,12 @@ public class DatabaseContext : DbContext
98149
public DatabaseContext(DbContextOptions options)
99150
: base(options)
100151
{
101-
this._provider = new MyCustomEncryptionProvider();
152+
_provider = new MyCustomEncryptionProvider();
102153
}
103154

104155
protected override void OnModelCreating(ModelBuilder modelBuilder)
105156
{
106-
modelBuilder.UseEncryption(this._provider);
157+
modelBuilder.UseEncryption(_provider);
107158
}
108159
}
109160
```

samples/AesSample.Fluent/AesSample.Fluent.csproj

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net6.0;net5.0</TargetFrameworks>
5+
<TargetFramework>net6.0</TargetFramework>
66
<LangVersion>10</LangVersion>
77
</PropertyGroup>
88

9-
<ItemGroup Condition="('$(TargetFramework)' == 'net5.0')">
10-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="[5,)" />
11-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="[5,)" />
12-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="[5,)" />
13-
</ItemGroup>
14-
<ItemGroup Condition="('$(TargetFramework)' == 'net6.0')">
15-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="[6,)" />
16-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="[6,)" />
17-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="[6,)" />
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.10" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.10" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.10" />
1813
</ItemGroup>
1914

2015
<ItemGroup>

samples/AesSample.Fluent/DatabaseContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.EntityFrameworkCore;
22
using Microsoft.EntityFrameworkCore.DataEncryption;
3+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
34
using System.ComponentModel.DataAnnotations;
45

56
namespace AesSample.Fluent;
@@ -18,7 +19,7 @@ public DatabaseContext(DbContextOptions<DatabaseContext> options, IEncryptionPro
1819

1920
protected override void OnModelCreating(ModelBuilder modelBuilder)
2021
{
21-
var userEntityBuilder = modelBuilder.Entity<UserEntity>();
22+
EntityTypeBuilder<UserEntity> userEntityBuilder = modelBuilder.Entity<UserEntity>();
2223

2324
userEntityBuilder.HasKey(x => x.Id);
2425
userEntityBuilder.Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();

samples/AesSample/AesSample.csproj

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
5+
<TargetFramework>net6.0</TargetFramework>
66
<LangVersion>10</LangVersion>
77
</PropertyGroup>
88

9-
<ItemGroup Condition="('$(TargetFramework)' == 'net5.0')">
10-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="[5,)" />
11-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="[5,)" />
12-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="[5,)" />
13-
</ItemGroup>
14-
<ItemGroup Condition="('$(TargetFramework)' == 'net6.0')">
15-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="[6,)" />
16-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="[6,)" />
17-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="[6,)" />
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.10" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.10" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.10" />
1813
</ItemGroup>
1914

2015
<ItemGroup>

0 commit comments

Comments
 (0)