Skip to content

Commit

Permalink
Update samples
Browse files Browse the repository at this point in the history
  • Loading branch information
Eastrall committed Jan 18, 2023
1 parent 9a52243 commit b56a363
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 31 deletions.
6 changes: 5 additions & 1 deletion samples/AesSample.Fluent/DatabaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
userEntityBuilder.Property(x => x.FirstName).IsRequired();
userEntityBuilder.Property(x => x.LastName).IsRequired();
userEntityBuilder.Property(x => x.Email).IsRequired().IsEncrypted();
userEntityBuilder.Property(x => x.Notes).IsRequired().HasColumnType("BLOB").IsEncrypted(StorageFormat.Binary);
userEntityBuilder.Property(x => x.EncryptedData).IsRequired().IsEncrypted();
userEntityBuilder.Property(x => x.EncryptedDataAsString).IsRequired().HasColumnType("TEXT").IsEncrypted(StorageFormat.Base64);

modelBuilder.UseEncryption(_encryptionProvider);
if (_encryptionProvider is not null)
{
modelBuilder.UseEncryption(_encryptionProvider);
}

base.OnModelCreating(modelBuilder);
}
Expand Down
11 changes: 11 additions & 0 deletions samples/AesSample.Fluent/EncryptedDatabaseContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.EntityFrameworkCore;

namespace AesSample.Fluent;

public class EncryptedDatabaseContext : DatabaseContext
{
public EncryptedDatabaseContext(DbContextOptions<DatabaseContext> options)
: base(options, null)
{
}
}
43 changes: 28 additions & 15 deletions samples/AesSample.Fluent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,38 @@ static void Main(string[] args)
byte[] encryptionIV = keyInfo.IV;
var encryptionProvider = new AesProvider(encryptionKey, encryptionIV);

using var context = new DatabaseContext(options, encryptionProvider);
context.Database.EnsureCreated();

var user = new UserEntity
using (var context = new DatabaseContext(options, encryptionProvider))
{
FirstName = "John",
LastName = "Doe",
Email = "[email protected]",
EncryptedData = new byte[2] { 1, 2 },
EncryptedDataAsString = new byte[2] { 3, 4 }
};
context.Database.EnsureCreated();

var user = new UserEntity
{
FirstName = "John",
LastName = "Doe",
Email = "[email protected]",
Notes = "Hello world!",
EncryptedData = new byte[2] { 1, 2 },
EncryptedDataAsString = new byte[2] { 3, 4 }
};

context.Users.Add(user);
context.SaveChanges();

context.Users.Add(user);
context.SaveChanges();
Console.WriteLine($"Users count: {context.Users.Count()}");
}

Console.WriteLine($"Users count: {context.Users.Count()}");
using (var context = new EncryptedDatabaseContext(options))
{
UserEntity user = context.Users.First();

Console.WriteLine($"Encrypted User: {user.FirstName} {user.LastName} - {user.Email} (Notes: {user.Notes})");
}

user = context.Users.First();
using (var context = new DatabaseContext(options, encryptionProvider))
{
UserEntity user = context.Users.First();

Console.WriteLine($"User: {user.FirstName} {user.LastName} - {user.Email}");
Console.WriteLine($"User: {user.FirstName} {user.LastName} - {user.Email} (Notes: {user.Notes})");
}
}
}
2 changes: 2 additions & 0 deletions samples/AesSample.Fluent/UserEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class UserEntity

public string Email { get; set; }

public string Notes { get; set; }

public byte[] EncryptedData { get; set; }

public byte[] EncryptedDataAsString { get; set; }
Expand Down
36 changes: 21 additions & 15 deletions samples/AesSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,31 @@ static void Main()
byte[] encryptionIV = keyInfo.IV;
var encryptionProvider = new AesProvider(encryptionKey, encryptionIV);

using var context = new DatabaseContext(options, encryptionProvider);
context.Database.EnsureCreated();

var user = new UserEntity
using (var context = new DatabaseContext(options, encryptionProvider))
{
FirstName = "John",
LastName = "Doe",
Email = "[email protected]",
EncryptedData = new byte[2] { 1, 2 },
EncryptedDataAsString = new byte[2] { 3, 4 }
};
context.Database.EnsureCreated();

var user = new UserEntity
{
FirstName = "John",
LastName = "Doe",
Email = "[email protected]",
Notes = "Hello world!",
EncryptedData = new byte[2] { 1, 2 },
EncryptedDataAsString = new byte[2] { 3, 4 }
};

context.Users.Add(user);
context.SaveChanges();
context.Users.Add(user);
context.SaveChanges();

Console.WriteLine($"Users count: {context.Users.Count()}");
Console.WriteLine($"Users count: {context.Users.Count()}");
}

user = context.Users.First();
using (var context = new DatabaseContext(options, encryptionProvider))
{
UserEntity user = context.Users.First();

Console.WriteLine($"User: {user.FirstName} {user.LastName} - {user.Email}");
Console.WriteLine($"User: {user.FirstName} {user.LastName} - {user.Email} (Notes: {user.Notes})");
}
}
}
4 changes: 4 additions & 0 deletions samples/AesSample/UserEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public class UserEntity
[Encrypted]
public string Email { get; set; }

[Required]
[Encrypted(StorageFormat.Binary)]
public string Notes { get; set; }

[Required]
[Encrypted]
public byte[] EncryptedData { get; set; }
Expand Down

0 comments on commit b56a363

Please sign in to comment.