-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Ability to create ICryptoTransform, passing key and iv as Span<byte> #27118
Comments
Creating a new object would have to copy the key and IV (Because the Span can't be copied to the fields of the new ICryptoTransform). We might want to consider encryption and decryption methods that don't require an ICryptoTransform and that accept Spans though. The API in dotnet/corefx#31389 could be a good general template for that. |
Really there are two different problems.
The first one could be solved by making overloads to CreateEncryptor and CreateDecryptor which took in The second part doesn't have a very good solution right now. In the BCL our compatibility and breaking change rules say that we can't alter an interface once it ships, so we can't add new methods to ICryptoTransform. Sometimes we can work around that with extension methods, but an extension method on an interface can usually only simplify the API, not make there being an alternate way of doing work. So all an extension method could do is call The way forward is most likely via default interface implementations (so new span-based methods can defer to the array-based ones, but then all of our implementations can re-define their behavior to make the span path more optimal). As far as I know, that isn't available to corefx yet. |
ToArray will create unnecessary allocations, but we can create an extension method as you mentioned, accepting Span or ReadOnlyMemory as an argument, then we could get ArraySegment from memory by calling MemoryMarshal.TryGetArray method, further we could get and access to array via arraysegment.Array property. |
The correct (in a pure world) design for the API is TransformFinalBlock might be tricky either way, since you can't really make the Span version call the current one and then return false because the buffer was too small (because data might get lost). The interface default version would (thinking out loud) need to return false without calling TransformFinalBlock if the output span was insufficient for the number of blocks required for the input, plus one more whole block. Definitely a place where the overrides would do better. |
There needs to be a new interface to properly support |
AES has fixed IV and key size. The data could be copied out into a field (not an array). |
It could be for example in Aes class, where CreateEncryptor declared, we could pass Span for internal UniversalCryptoTransformer.
The text was updated successfully, but these errors were encountered: