Skip to content

Commit bab196a

Browse files
committed
prettify
1 parent 0d30a5b commit bab196a

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

src/DataProtection/DataProtection/src/Internal/DataProtectionPool.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
using System.Threading.Tasks;
1010

1111
namespace Microsoft.AspNetCore.DataProtection.Internal;
12+
13+
/// <summary>
14+
/// Used for pooling secret data (e.g. Protect()/Unprotect() flow).
15+
/// Main goal is not to intersect with the <see cref="ArrayPool{T}.Shared"/>
16+
/// </summary>
1217
internal static class DataProtectionPool
1318
{
1419
private static readonly ArrayPool<byte> _pool = ArrayPool<byte>.Create();

src/DataProtection/DataProtection/src/Managed/ManagedAuthenticatedEncryptor.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,6 @@ public byte[] Decrypt(ArraySegment<byte> protectedPayload, ArraySegment<byte> ad
197197

198198
// Step 2: Decrypt the KDK and use it to restore the original encryption and MAC keys.
199199

200-
// The best optimization is to stackalloc. If the size is too big, we would want to rent from the pool,
201-
// but we can't due to the HashAlgorithm, ValidationAlgorithm and SymmetricAlgorithm requiring a byte[] instead of a Span<byte>
202-
// in the constructor / Key property.
203200
#if NET10_0_OR_GREATER
204201
byte[]? decryptedKdkLease = null;
205202
Span<byte> decryptedKdk = _keyDerivationKey.Length <= 128
@@ -209,6 +206,11 @@ public byte[] Decrypt(ArraySegment<byte> protectedPayload, ArraySegment<byte> ad
209206
var decryptedKdk = new byte[_keyDerivationKey.Length];
210207
#endif
211208

209+
// The best optimization is to stackalloc. If the size is too big, we would want to rent from the pool,
210+
// but we can't due to the ValidationAlgorithm and SymmetricAlgorithm requiring a byte[] instead of a Span<byte>
211+
// in the constructor / Key property.
212+
// Also .Rent() returns an array of approximately the same size (for input 24 it will be 32 in example)
213+
// but in this code we need to slice it (again it will be Span<byte>) which is not compatible with APIS
212214
var decryptionSubkey = new byte[_symmetricAlgorithmSubkeyLengthInBytes];
213215
var validationSubkey = new byte[_validationAlgorithmSubkeyLengthInBytes];
214216

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
using Microsoft.Extensions.DependencyInjection;
8+
9+
namespace Microsoft.AspNetCore.DataProtection.Tests;
10+
public class E2ETests
11+
{
12+
[Fact]
13+
public void ProtectAndUnprotect_ForSampleAntiforgeryToken()
14+
{
15+
const string sampleToken = "CfDJ8H5oH_fp1QNBmvs-OWXxsVoV30hrXeI4-PI4p1VZytjsgd0DTstMdtTZbFtm2dKHvsBlDCv7TiEWKztZf8fb48pUgBgUE2SeYV3eOUXvSfNWU0D8SmHLy5KEnwKKkZKqudDhCnjQSIU7mhDliJJN1e4";
16+
17+
var dataProtector = GetServiceCollectionBuiltDataProtector();
18+
var encrypted = dataProtector.Protect(sampleToken);
19+
var decrypted = dataProtector.Unprotect(encrypted);
20+
Assert.Equal(sampleToken, decrypted);
21+
}
22+
23+
private static IDataProtector GetServiceCollectionBuiltDataProtector(string purpose = "samplePurpose")
24+
=> new ServiceCollection()
25+
.AddDataProtection()
26+
.Services.BuildServiceProvider()
27+
.GetDataProtector(purpose);
28+
}

src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/KeyManagement/KeyRingBasedDataProtectorTests.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -619,23 +619,6 @@ public void CreateProtector_ChainsPurposes()
619619
Assert.Equal(expectedProtectedData, retVal);
620620
}
621621

622-
[Fact]
623-
public void Test()
624-
{
625-
const string sampleToken = "CfDJ8H5oH_fp1QNBmvs-OWXxsVoV30hrXeI4-PI4p1VZytjsgd0DTstMdtTZbFtm2dKHvsBlDCv7TiEWKztZf8fb48pUgBgUE2SeYV3eOUXvSfNWU0D8SmHLy5KEnwKKkZKqudDhCnjQSIU7mhDliJJN1e4";
626-
627-
var dataProtector = GetServiceCollectionBuiltDataProtector();
628-
var encrypted = dataProtector.Protect(sampleToken);
629-
var decrypted = dataProtector.Unprotect(encrypted);
630-
Assert.Equal(sampleToken, decrypted);
631-
}
632-
633-
private static IDataProtector GetServiceCollectionBuiltDataProtector()
634-
=> new ServiceCollection()
635-
.AddDataProtection()
636-
.Services.BuildServiceProvider()
637-
.GetDataProtector("SamplePurpose");
638-
639622
private static byte[] BuildAadFromPurposeStrings(Guid keyId, params string[] purposes)
640623
{
641624
var expectedAad = new byte[] { 0x09, 0xF0, 0xC9, 0xF0 } // magic header

0 commit comments

Comments
 (0)