Skip to content

Commit e0615d6

Browse files
committed
WIP: Version 4 rework
1 parent de814ed commit e0615d6

38 files changed

+1026
-2040
lines changed

.editorconfig

Lines changed: 0 additions & 39 deletions
This file was deleted.

EntityFrameworkCore.DataEncryption.sln

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.30804.86
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.3.32901.215
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{3EC10767-1816-46B2-A78E-9856071CCFDB}"
77
EndProject
@@ -22,15 +22,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{64C3
2222
EndProject
2323
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AesSample", "samples\AesSample\AesSample.csproj", "{8AA1E576-4016-4623-96C8-90330F05F9A8}"
2424
EndProject
25-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".azure", ".azure", "{073FEA06-67CF-47F8-8CE4-2B153A7D8443}"
25+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{EEF46CDC-C438-48FC-BEF7-83AEE26C63F7}"
2626
EndProject
27-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "pipelines", "pipelines", "{68558245-F605-413F-A1D9-A4F60D489D68}"
27+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{4F549FEF-C57B-4A34-A2C7-8A632762DF85}"
2828
ProjectSection(SolutionItems) = preProject
29-
.azure\pipelines\azure-pipelines.yml = .azure\pipelines\azure-pipelines.yml
30-
EndProjectSection
31-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{5EE4E8BE-6B15-49DB-A4A8-D2CD63D5E90C}"
32-
ProjectSection(SolutionItems) = preProject
33-
.editorconfig = .editorconfig
29+
.github\workflows\build.yml = .github\workflows\build.yml
3430
EndProjectSection
3531
EndProject
3632
Global
@@ -59,8 +55,8 @@ Global
5955
{D037F8D0-E606-4C5A-8669-DB6AAE7B056B} = {3EC10767-1816-46B2-A78E-9856071CCFDB}
6056
{5E023B6A-0B47-4EC2-90B9-2DF998E58ADB} = {E4089551-AF4E-41B3-A6F8-2501A3BE0E0C}
6157
{8AA1E576-4016-4623-96C8-90330F05F9A8} = {64C3D7D1-67B8-4070-AE67-C71B761535CC}
62-
{073FEA06-67CF-47F8-8CE4-2B153A7D8443} = {3A8D800E-77BD-44EF-82DB-C672281ECAAA}
63-
{68558245-F605-413F-A1D9-A4F60D489D68} = {073FEA06-67CF-47F8-8CE4-2B153A7D8443}
58+
{EEF46CDC-C438-48FC-BEF7-83AEE26C63F7} = {3A8D800E-77BD-44EF-82DB-C672281ECAAA}
59+
{4F549FEF-C57B-4A34-A2C7-8A632762DF85} = {EEF46CDC-C438-48FC-BEF7-83AEE26C63F7}
6460
EndGlobalSection
6561
GlobalSection(ExtensibilityGlobals) = postSolution
6662
SolutionGuid = {4997BAE9-29BF-4D79-AE5E-5605E7A0F049}

samples/AesSample/AesSample.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
6+
<LangVersion>10</LangVersion>
67
</PropertyGroup>
78

89
<ItemGroup Condition="('$(TargetFramework)' == 'net5.0')">
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
using Microsoft.EntityFrameworkCore;
22
using Microsoft.EntityFrameworkCore.DataEncryption;
33

4-
namespace AesSample
4+
namespace AesSample;
5+
6+
public class DatabaseContext : DbContext
57
{
6-
public class DatabaseContext : DbContext
7-
{
8-
private readonly IEncryptionProvider _encryptionProvider;
8+
private readonly IEncryptionProvider _encryptionProvider;
99

10-
public DbSet<UserEntity> Users { get; set; }
10+
public DbSet<UserEntity> Users { get; set; }
1111

12-
public DatabaseContext(DbContextOptions<DatabaseContext> options, IEncryptionProvider encryptionProvider)
13-
: base(options)
14-
{
15-
_encryptionProvider = encryptionProvider;
16-
}
12+
public DatabaseContext(DbContextOptions<DatabaseContext> options, IEncryptionProvider encryptionProvider)
13+
: base(options)
14+
{
15+
_encryptionProvider = encryptionProvider;
16+
}
1717

18-
protected override void OnModelCreating(ModelBuilder modelBuilder)
19-
{
20-
modelBuilder.UseEncryption(_encryptionProvider);
18+
protected override void OnModelCreating(ModelBuilder modelBuilder)
19+
{
20+
modelBuilder.UseEncryption(_encryptionProvider);
2121

22-
base.OnModelCreating(modelBuilder);
23-
}
22+
base.OnModelCreating(modelBuilder);
2423
}
2524
}

samples/AesSample/Program.cs

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,54 @@
44
using System.Linq;
55
using System.Security;
66

7-
namespace AesSample
7+
namespace AesSample;
8+
9+
static class Program
810
{
9-
static class Program
11+
static void Main()
1012
{
11-
static void Main()
12-
{
13-
var options = new DbContextOptionsBuilder<DatabaseContext>()
14-
.UseInMemoryDatabase(databaseName: "MyInMemoryDatabase")
15-
.Options;
13+
var options = new DbContextOptionsBuilder<DatabaseContext>()
14+
.UseInMemoryDatabase(databaseName: "MyInMemoryDatabase")
15+
.Options;
1616

17-
// AES key randomly generated at each run.
18-
byte[] encryptionKey = AesProvider.GenerateKey(AesKeySize.AES256Bits).Key;
19-
var encryptionProvider = new AesProvider(encryptionKey);
17+
// AES key randomly generated at each run.
18+
AesKeyInfo keyInfo = AesProvider.GenerateKey(AesKeySize.AES256Bits);
19+
byte[] encryptionKey = keyInfo.Key;
20+
byte[] encryptionIV = keyInfo.IV;
21+
var encryptionProvider = new AesProvider(encryptionKey, encryptionIV);
2022

21-
using var context = new DatabaseContext(options, encryptionProvider);
23+
using var context = new DatabaseContext(options, encryptionProvider);
2224

23-
var user = new UserEntity
24-
{
25-
FirstName = "John",
26-
LastName = "Doe",
27-
Email = "[email protected]",
28-
Password = BuildPassword(),
29-
};
25+
var user = new UserEntity
26+
{
27+
FirstName = "John",
28+
LastName = "Doe",
29+
Email = "[email protected]",
30+
//Password = BuildPassword(),
31+
};
3032

31-
context.Users.Add(user);
32-
context.SaveChanges();
33+
context.Users.Add(user);
34+
context.SaveChanges();
3335

34-
Console.WriteLine($"Users count: {context.Users.Count()}");
36+
Console.WriteLine($"Users count: {context.Users.Count()}");
3537

36-
user = context.Users.First();
38+
user = context.Users.First();
3739

38-
Console.WriteLine($"User: {user.FirstName} {user.LastName} - {user.Email} ({user.Password.Length})");
39-
}
40+
Console.WriteLine($"User: {user.FirstName} {user.LastName} - {user.Email}");
41+
}
4042

41-
static SecureString BuildPassword()
42-
{
43-
SecureString result = new();
44-
result.AppendChar('L');
45-
result.AppendChar('e');
46-
result.AppendChar('t');
47-
result.AppendChar('M');
48-
result.AppendChar('e');
49-
result.AppendChar('I');
50-
result.AppendChar('n');
51-
result.AppendChar('!');
52-
result.MakeReadOnly();
53-
return result;
54-
}
43+
static SecureString BuildPassword()
44+
{
45+
SecureString result = new();
46+
result.AppendChar('L');
47+
result.AppendChar('e');
48+
result.AppendChar('t');
49+
result.AppendChar('M');
50+
result.AppendChar('e');
51+
result.AppendChar('I');
52+
result.AppendChar('n');
53+
result.AppendChar('!');
54+
result.MakeReadOnly();
55+
return result;
5556
}
5657
}

samples/AesSample/UserEntity.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,23 @@
33
using System.ComponentModel.DataAnnotations.Schema;
44
using System.Security;
55

6-
namespace AesSample
6+
namespace AesSample;
7+
8+
public class UserEntity
79
{
8-
public class UserEntity
9-
{
10-
[Key]
11-
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
12-
public Guid Id { get; set; }
10+
[Key]
11+
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
12+
public Guid Id { get; set; }
1313

14-
[Required]
15-
public string FirstName { get; set; }
14+
[Required]
15+
public string FirstName { get; set; }
1616

17-
[Required]
18-
public string LastName { get; set; }
17+
[Required]
18+
public string LastName { get; set; }
1919

20-
[Required]
21-
[Encrypted]
22-
public string Email { get; set; }
20+
[Required]
21+
[Encrypted]
22+
public string Email { get; set; }
2323

24-
public SecureString Password { get; set; }
25-
}
24+
//public SecureString Password { get; set; }
2625
}
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
namespace System.ComponentModel.DataAnnotations
1+
namespace System.ComponentModel.DataAnnotations;
2+
3+
/// <summary>
4+
/// Specifies that the data field value should be encrypted.
5+
/// </summary>
6+
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
7+
public sealed class EncryptedAttribute : Attribute
28
{
39
/// <summary>
4-
/// Specifies that the data field value should be encrypted.
10+
/// Returns the storage format for the database value.
511
/// </summary>
6-
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
7-
public sealed class EncryptedAttribute : Attribute
8-
{
9-
/// <summary>
10-
/// Initializes a new instance of the <see cref="EncryptedAttribute"/> class.
11-
/// </summary>
12-
/// <param name="format">
13-
/// The storage format.
14-
/// </param>
15-
public EncryptedAttribute(StorageFormat format)
16-
{
17-
Format = format;
18-
}
12+
public StorageFormat Format { get; }
1913

20-
/// <summary>
21-
/// Initializes a new instance of the <see cref="EncryptedAttribute"/> class.
22-
/// </summary>
23-
public EncryptedAttribute() : this(StorageFormat.Default)
24-
{
25-
}
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="EncryptedAttribute"/> class.
16+
/// </summary>
17+
/// <param name="format">
18+
/// The storage format.
19+
/// </param>
20+
public EncryptedAttribute(StorageFormat format)
21+
{
22+
Format = format;
23+
}
2624

27-
/// <summary>
28-
/// Returns the storage format for the database value.
29-
/// </summary>
30-
public StorageFormat Format { get; }
25+
/// <summary>
26+
/// Initializes a new instance of the <see cref="EncryptedAttribute"/> class.
27+
/// </summary>
28+
public EncryptedAttribute()
29+
: this(StorageFormat.Default)
30+
{
3131
}
3232
}
Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
namespace System.ComponentModel.DataAnnotations
1+
namespace System.ComponentModel.DataAnnotations;
2+
3+
/// <summary>
4+
/// Represents the storage format for an encrypted value.
5+
/// </summary>
6+
public enum StorageFormat
27
{
38
/// <summary>
4-
/// Represents the storage format for an encrypted value.
9+
/// The format is determined by the model data type.
510
/// </summary>
6-
public enum StorageFormat
7-
{
8-
/// <summary>
9-
/// The format is determined by the model data type.
10-
/// </summary>
11-
Default,
12-
/// <summary>
13-
/// The value is stored in binary.
14-
/// </summary>
15-
Binary,
16-
/// <summary>
17-
/// The value is stored in a Base64-encoded string.
18-
/// </summary>
19-
/// <remarks>
20-
/// <b>NB:</b> If the source property is a <see cref="string"/>,
21-
/// and no encryption provider is configured,
22-
/// the string will not be modified.
23-
/// </remarks>
24-
Base64,
25-
}
11+
Default,
12+
13+
/// <summary>
14+
/// The value is stored in binary.
15+
/// </summary>
16+
Binary,
17+
18+
/// <summary>
19+
/// The value is stored in a Base64-encoded string.
20+
/// </summary>
21+
/// <remarks>
22+
/// <b>NB:</b> If the source property is a <see cref="string"/>,
23+
/// and no encryption provider is configured,
24+
/// the string will not be modified.
25+
/// </remarks>
26+
Base64,
2627
}

0 commit comments

Comments
 (0)