-
Notifications
You must be signed in to change notification settings - Fork 10.4k
API proposal: Add ReadOnlySpan<byte> to IDataProtector (un)Protect #44758
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
Comments
@KLuuKer We're interested in learning more about your use case. Do you plan to use it with Note: We'd have to ifdef the new API out of netstandard unless we make a whole new interface because netstandard does not support default interface methods, and we cannot make breaking changes to this interface. |
Thanks for contacting us. We're moving this issue to the |
Triage: optimizing this is likely going to help in some hot paths. We believe the API needs some design work though. Note: We'll need to make sure netstandard2.0 works. |
@halter73 yes I do use it with Sometimes I also end up writing custom byte's rented from a Can't wait until net8.0 so I can start using it |
Maybe something like a |
My team's services would benefit from this API as well. Our scenario involves StackExchange.Redis, where the values we want to encrypt are of type I think it would be sufficient for us to call |
To get the perf win, we'd have to avoid allocating the array in the API implementation as well, so we'd probably need/want to update these too:
And probably these, if only for consistency:
|
This is a pretty big change, but we'll give it further consideration in 9.0. |
Just weighing in to support this proposal. Right now I am writing some serialised data to a cookie which I'd like to protect. While I can do much of the serialisation without allocating by using rented arrays and spans, etc., as soon as I need to protect that data, I'm forced to allocate the |
I am working on Antiforgery improvements and it uses
We can improve there only if we make a change in DataProtection layer: so far for example validation scenario in Antiforgery cant be impacted due to ProposalI reviewed all the new APIs we have to expose and here they are: namespace Microsoft.AspNetCore.DataProtection;
public interface IDataProtector : IDataProtectionProvider
{
+ bool TryProtect(ReadOnlySpan<byte> plainText, Span<byte> destination, out int bytesWritten);
+ bool TryUnprotect(ReadOnlySpan<byte> protectedData, Span<byte> destination, out int bytesWritten);
}
namespace Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption;
public interface IAuthenticatedEncryptor
{
+ bool TryDecrypt(ReadOnlySpan<byte> cipherText, ReadOnlySpan<byte> additionalAuthenticatedData, Span<byte> destination, out int bytesWritten);
+ bool TryEncrypt(ReadOnlySpan<byte> plaintText, ReadOnlySpan<byte> additionalAuthenticatedData, Span<byte> destination, out int bytesWritten);
} Other APIsI reviewed proposition by @amcasey and I think those can be left for now ( aspnetcore/src/DataProtection/DataProtection/src/Managed/ManagedAuthenticatedEncryptor.cs Line 194 in 08cff58
these ones should be also recreated with
there are also other types of
|
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
concerns from @BrennanConroy:
|
This is looking good, but we want to have a better understanding if we need the public interface IDataProtector : IDataProtectionProvider
{
+ #if NET
+ bool TryProtect(ReadOnlySpan<byte> plainText, Span<byte> destination, out int bytesWritten);
+ bool TryUnprotect(ReadOnlySpan<byte> protectedData, Span<byte> destination, out int bytesWritten);
+ #endif
}
public interface IAuthenticatedEncryptor
{
+ #if NET
+ bool TryDecrypt(ReadOnlySpan<byte> cipherText, ReadOnlySpan<byte> additionalAuthenticatedData, Span<byte> destination, out int bytesWritten);
+ bool TryEncrypt(ReadOnlySpan<byte> plaintText, ReadOnlySpan<byte> additionalAuthenticatedData, Span<byte> destination, out int bytesWritten);
+ #endif
} |
Background and Motivation
Add
ReadOnlySpan<byte>
overrides so consumer apps are able to avoid a extra array allocation+blockbuffer.copy every time they try to protect\unprotect data.Proposed API
Usage Examples
Alternative Designs
Risks
All of the existing protector implementations have to be changed to implement this change.
We could also have a default interface implementation to prevent it breaking to aggressively.
Performance impact should be minimal, and actually open the possibility for apps to avoid a unnecessary array copy, if people use the api correctly
The text was updated successfully, but these errors were encountered: