Skip to content

Commit 96e169b

Browse files
committed
Remoive secure string support
1 parent ff0dd50 commit 96e169b

File tree

6 files changed

+43
-271
lines changed

6 files changed

+43
-271
lines changed

src/EntityFrameworkCore.DataEncryption/Internal/EncryptionConverter.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
22
using System;
33
using System.ComponentModel.DataAnnotations;
4-
using System.Security;
54
using System.Text;
65

76
namespace Microsoft.EntityFrameworkCore.DataEncryption.Internal;
@@ -33,7 +32,6 @@ private static TOutput Encrypt<TInput, TOutput>(TInput input, IEncryptionProvide
3332
{
3433
string => Encoding.UTF8.GetBytes(input.ToString()),
3534
byte[] => input as byte[],
36-
SecureString => null,
3735
_ => null,
3836
};
3937

@@ -69,10 +67,6 @@ private static TModel Decrypt<TInput, TOupout>(TProvider input, IEncryptionProvi
6967
{
7068
decryptedData = decryptedRawBytes;
7169
}
72-
else if (destinationType == typeof(SecureString))
73-
{
74-
// TODO
75-
}
7670

7771
return (TModel)Convert.ChangeType(decryptedData, typeof(TModel));
7872
}

src/EntityFrameworkCore.DataEncryption/ModelBuilderExtensions.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.ComponentModel.DataAnnotations;
88
using System.Linq;
99
using System.Reflection;
10-
using System.Security;
1110

1211
namespace Microsoft.EntityFrameworkCore.DataEncryption;
1312

@@ -80,10 +79,6 @@ private static ValueConverter GetValueConverter(Type propertyType, IEncryptionPr
8079
_ => throw new NotImplementedException()
8180
};
8281
}
83-
else if (propertyType == typeof(SecureString))
84-
{
85-
// TODO
86-
}
8782

8883
return null;
8984
}
@@ -93,20 +88,17 @@ private static IEnumerable<EncryptedProperty> GetEntityEncryptedProperties(IMuta
9388
return entity.GetProperties()
9489
.Select(p => new { Property = p, EncryptedAttribute = p.PropertyInfo?.GetCustomAttribute<EncryptedAttribute>(false) })
9590
.Where(x => x.EncryptedAttribute != null)
96-
.Select(x => new EncryptedProperty(entity, x.Property, x.EncryptedAttribute.Format));
91+
.Select(x => new EncryptedProperty(x.Property, x.EncryptedAttribute.Format));
9792
}
9893

9994
internal struct EncryptedProperty
10095
{
101-
public IMutableEntityType EntityType { get; }
102-
10396
public IMutableProperty Property { get; }
10497

10598
public StorageFormat StorageFormat { get; }
10699

107-
public EncryptedProperty(IMutableEntityType entityType, IMutableProperty property, StorageFormat storageFormat)
100+
public EncryptedProperty(IMutableProperty property, StorageFormat storageFormat)
108101
{
109-
EntityType = entityType;
110102
Property = property;
111103
StorageFormat = storageFormat;
112104
}

src/EntityFrameworkCore.DataEncryption/ModelExtensions.cs

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

src/EntityFrameworkCore.DataEncryption/PropertyBuilderExtensions.cs

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

src/EntityFrameworkCore.DataEncryption/Providers/AesProvider.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Microsoft.EntityFrameworkCore.DataEncryption.Internal.Extensions;
2-
using System.IO;
1+
using System.IO;
32
using System.Security.Cryptography;
43

54
namespace Microsoft.EntityFrameworkCore.DataEncryption.Providers;

test/EntityFrameworkCore.DataEncryption.Test/Providers/AesProviderTest.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Bogus;
22
using Microsoft.EntityFrameworkCore.DataEncryption.Providers;
33
using Microsoft.EntityFrameworkCore.DataEncryption.Test.Context;
4+
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67
using System.Security.Cryptography;
@@ -12,6 +13,26 @@ public class AesProviderTest
1213
{
1314
private readonly Faker _faker = new();
1415

16+
[Fact]
17+
public void EncryptNullOrEmptyDataTest()
18+
{
19+
AesKeyInfo encryptionKeyInfo = AesProvider.GenerateKey(AesKeySize.AES256Bits);
20+
var provider = new AesProvider(encryptionKeyInfo.Key, encryptionKeyInfo.IV);
21+
22+
Assert.Null(provider.Encrypt(null));
23+
Assert.Null(provider.Encrypt(Array.Empty<byte>()));
24+
}
25+
26+
[Fact]
27+
public void DecryptNullOrEmptyDataTest()
28+
{
29+
AesKeyInfo encryptionKeyInfo = AesProvider.GenerateKey(AesKeySize.AES256Bits);
30+
var provider = new AesProvider(encryptionKeyInfo.Key, encryptionKeyInfo.IV);
31+
32+
Assert.Null(provider.Decrypt(null));
33+
Assert.Null(provider.Decrypt(Array.Empty<byte>()));
34+
}
35+
1536
[Theory]
1637
[InlineData(AesKeySize.AES128Bits)]
1738
[InlineData(AesKeySize.AES192Bits)]
@@ -31,6 +52,25 @@ public void EncryptDecryptByteArrayTest(AesKeySize keySize)
3152
Assert.Equal(input, decryptedData);
3253
}
3354

55+
[Theory]
56+
[InlineData(AesKeySize.AES128Bits)]
57+
[InlineData(AesKeySize.AES192Bits)]
58+
[InlineData(AesKeySize.AES256Bits)]
59+
public void EncryptDecryptByteArrayWithoutIVTest(AesKeySize keySize)
60+
{
61+
byte[] input = _faker.Random.Bytes(_faker.Random.Int(10, 30));
62+
AesKeyInfo encryptionKeyInfo = AesProvider.GenerateKey(keySize);
63+
var provider = new AesProvider(encryptionKeyInfo.Key, null);
64+
65+
byte[] encryptedData = provider.Encrypt(input);
66+
Assert.NotNull(encryptedData);
67+
68+
byte[] decryptedData = provider.Decrypt(encryptedData);
69+
Assert.NotNull(decryptedData);
70+
71+
Assert.Equal(input, decryptedData);
72+
}
73+
3474
[Theory]
3575
[InlineData(AesKeySize.AES128Bits)]
3676
[InlineData(AesKeySize.AES192Bits)]

0 commit comments

Comments
 (0)