API proposal: Add ReadOnlySpan<byte> to IDataProtector (un)Protect
See original GitHub issueBackground 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
namespace Microsoft.AspNetCore.DataProtection;
public interface IDataProtector : IDataProtectionProvider
{
+ byte[] Protect(ReadOnlySpan<byte> plaintext);
+ byte[] Unprotect(ReadOnlySpan<byte> protectedData);
}
Usage Examples
var buffer = new ArrayBufferWriter<byte>();
// write some data to buffer
var blob = new Data { Some = "Payload" };
MessagePackSerializer.Serialize(buffer, blob);
IDataProtector protector = provider.CreateProtector("demo");
return protector.Protect(buffer.WrittenSpan);
Alternative Designs
namespace Microsoft.AspNetCore.DataProtection;
public interface IDataProtector : IDataProtectionProvider
{
+ byte[] Protect(ReadOnlySpan<byte> plaintext) => this.Protect(plaintext.ToArray())
+ byte[] Unprotect(ReadOnlySpan<byte> protectedData) => this.Unprotect(protectedData.ToArray())
}
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
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:7 (4 by maintainers)
Top Results From Across the Web
IDataProtector.Unprotect(Byte[]) Method
Cryptographically unprotects a piece of protected data.
Read more >Protecting Data with IDataProtector in ASP.NET Core
In this article, we are going to learn how to use IDataProtector to protect the sensitive data in the ASP.NET Core application.
Read more >How to Disable Data Protection in ASP.NET Core
To create a IDataProtector without protection you just need to return the same argument received in the methods Protect and Unprotect.
Read more >c# - IDataProtector unable to decrypt when application runs ...
json as I wrote some probing code that checks if the file is present and can be read before I even try to...
Read more >How to use the Data Protection API in ASP.NET Core
Configure the Data Protection API in ASP.NET Core ... The AddDataProtection extension method can be used to configure the Data Protection API. The ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
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.