Skip to content

Commit

Permalink
Merge pull request #52 from Eastrall/bugfix/nul-empty-string-encryption
Browse files Browse the repository at this point in the history
Fix null or empty string encryption
  • Loading branch information
Eastrall authored May 12, 2023
2 parents 6dc25da + 9e15a88 commit ea6836b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<AssemblyName>EntityFrameworkCore.DataEncryption</AssemblyName>
<RootNamespace>Microsoft.EntityFrameworkCore.DataEncryption</RootNamespace>
<IsPackable>true</IsPackable>
<Version>4.0.0</Version>
<Version>4.0.1</Version>
<Authors>Filipe GOMES PEIXOTO</Authors>
<PackageId>EntityFrameworkCore.DataEncryption</PackageId>
<PackageProjectUrl>https://github.com/Eastrall/EntityFrameworkCore.DataEncryption</PackageProjectUrl>
Expand All @@ -20,7 +20,7 @@
<Copyright>Filipe GOMES PEIXOTO © 2019 - 2023</Copyright>
<Description>A plugin for Microsoft.EntityFrameworkCore to add support of encrypted fields using built-in or custom encryption providers.</Description>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageReleaseNotes>https://github.com/Eastrall/EntityFrameworkCore.DataEncryption/releases/tag/v4.0.0</PackageReleaseNotes>
<PackageReleaseNotes>https://github.com/Eastrall/EntityFrameworkCore.DataEncryption/releases/tag/v4.0.1</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ private static TOutput Encrypt<TInput, TOutput>(TInput input, IEncryptionProvide
{
byte[] inputData = input switch
{
string => Encoding.UTF8.GetBytes(input.ToString()),
string => !string.IsNullOrEmpty(input.ToString()) ? Encoding.UTF8.GetBytes(input.ToString()) : null,
byte[] => input as byte[],
_ => null,
};

byte[] encryptedRawBytes = encryptionProvider.Encrypt(inputData);

if (encryptedRawBytes is null)
{
return default;
}

object encryptedData = storageFormat switch
{
StorageFormat.Default or StorageFormat.Base64 => Convert.ToBase64String(encryptedRawBytes),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public void PropertyShouldHaveEncryptionAnnotationsTest()
Name = name,
NameAsBytes = name,
ExtraData = bytes,
ExtraDataAsBytes = bytes
ExtraDataAsBytes = bytes,
EmptyString = ""
};

using var contextFactory = new DatabaseContextFactory();
Expand All @@ -52,6 +53,7 @@ public void PropertyShouldHaveEncryptionAnnotationsTest()
AssertPropertyAnnotations(entityType.GetProperty(nameof(UserEntity.ExtraData)), true, StorageFormat.Base64);
AssertPropertyAnnotations(entityType.GetProperty(nameof(UserEntity.ExtraDataAsBytes)), true, StorageFormat.Binary);
AssertPropertyAnnotations(entityType.GetProperty(nameof(UserEntity.Id)), false, StorageFormat.Default);
AssertPropertyAnnotations(entityType.GetProperty(nameof(UserEntity.EmptyString)), true, StorageFormat.Base64);

context.Users.Add(user);
context.SaveChanges();
Expand All @@ -66,6 +68,7 @@ public void PropertyShouldHaveEncryptionAnnotationsTest()
Assert.Equal(name, u.NameAsBytes);
Assert.Equal(bytes, u.ExtraData);
Assert.Equal(bytes, u.ExtraDataAsBytes);
Assert.Null(u.EmptyString);
}
}

Expand Down Expand Up @@ -106,6 +109,9 @@ private class UserEntity

// Encrypted as raw byte array.
public byte[] ExtraDataAsBytes { get; set; }

// Encrypt as Base64 string, but will be empty.
public string EmptyString { get; set; }
}

private class FluentDbContext : DbContext
Expand Down Expand Up @@ -134,6 +140,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
userEntityBuilder.Property(x => x.NameAsBytes).IsRequired().HasColumnType("BLOB").IsEncrypted(StorageFormat.Binary);
userEntityBuilder.Property(x => x.ExtraData).IsRequired().HasColumnType("TEXT").IsEncrypted(StorageFormat.Base64);
userEntityBuilder.Property(x => x.ExtraDataAsBytes).IsRequired().HasColumnType("BLOB").IsEncrypted(StorageFormat.Binary);
userEntityBuilder.Property(x => x.EmptyString).IsRequired(false).HasColumnType("TEXT").IsEncrypted(StorageFormat.Base64);

modelBuilder.UseEncryption(_encryptionProvider);
}
Expand Down

0 comments on commit ea6836b

Please sign in to comment.