-
Notifications
You must be signed in to change notification settings - Fork 10.5k
perf: improve ManagedAuthenticatedEncryptor Decrypt() and Encrypt() flow #59424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 18 commits
e47b41d
bb6d3c0
384c3d5
90bf754
a37b0d5
268ee44
eb6cb1b
d090882
6ec76d3
49eab48
4ba9cc2
919d003
87d118b
2524a15
ec70b6e
0d30a5b
bab196a
e96ad0c
cfbf7a5
c9aa2e3
560c79e
8e3df1a
4434822
aed8e9d
a846056
e72f716
cf48cc8
0f3d1aa
51bfe77
ab25e0f
6f035d4
22224dc
c8019fb
d87aeaf
489c5cd
26ee6ad
9ce2b17
f50c7fb
f45a948
66b2170
66cd115
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Buffers; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.AspNetCore.DataProtection.Internal; | ||
|
||
/// <summary> | ||
/// Used for pooling secret data (e.g. Protect()/Unprotect() flow). | ||
/// Main goal is not to intersect with the <see cref="ArrayPool{T}.Shared"/> | ||
/// </summary> | ||
internal static class DataProtectionPool | ||
{ | ||
private static readonly ArrayPool<byte> _pool = ArrayPool<byte>.Create(); | ||
|
||
public static byte[] Rent(int length) => _pool.Rent(length); | ||
public static void Return(byte[] array, bool clearArray = false) => _pool.Return(array, clearArray); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
|
||
namespace Microsoft.AspNetCore.DataProtection.Managed; | ||
|
||
internal interface IManagedGenRandom | ||
{ | ||
byte[] GenRandom(int numBytes); | ||
|
||
#if NET10_0_OR_GREATER | ||
void GenRandom(Span<byte> target); | ||
#endif | ||
} |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.DataProtection.Tests; | ||
public class E2ETests | ||
{ | ||
[Fact] | ||
public void ProtectAndUnprotect_ForSampleAntiforgeryToken() | ||
|
||
{ | ||
const string sampleToken = "CfDJ8H5oH_fp1QNBmvs-OWXxsVoV30hrXeI4-PI4p1VZytjsgd0DTstMdtTZbFtm2dKHvsBlDCv7TiEWKztZf8fb48pUgBgUE2SeYV3eOUXvSfNWU0D8SmHLy5KEnwKKkZKqudDhCnjQSIU7mhDliJJN1e4"; | ||
|
||
var dataProtector = GetServiceCollectionBuiltDataProtector(); | ||
var encrypted = dataProtector.Protect(sampleToken); | ||
var decrypted = dataProtector.Unprotect(encrypted); | ||
Assert.Equal(sampleToken, decrypted); | ||
} | ||
|
||
private static IDataProtector GetServiceCollectionBuiltDataProtector(string purpose = "samplePurpose") | ||
=> new ServiceCollection() | ||
.AddDataProtection() | ||
.Services.BuildServiceProvider() | ||
.GetDataProtector(purpose); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.