Skip to content

Span<byte> on ICryptoTransform #38764

@vcsjones

Description

@vcsjones

Background and Motivation

The current APIs on ICryptoTransform are byte[] based, and accept an offset and length. This pattern is better represented as a Span<byte> today, as developers are more familiar with Span and expect these APIs where the previous pattern was used.

For TransformFinalBlock, a byte array is returned, which forces the implementation to allocate on return. The Span based implementations could allow transformation in to an existing buffer, or in-place.

Proposed API

namespace System.Security.Cryptography {
    public partial interface ICryptoTransform {
+        int TransformBlock(ReadOnlySpan<byte> inputBuffer, Span<byte> outputBuffer);
+        bool TryTransformBlock(ReadOnlySpan<byte> inputBuffer, Span<byte> outputBuffer, out int bytesWritten);

+        int TransformFinalBlock(ReadOnlySpan<byte> inputBuffer, Span<byte> outputBuffer);
+        bool TryTransformFinalBlock(ReadOnlySpan<byte> inputBuffer, Span<byte> outputBuffer, out int bytesWritten);
    }
}

Risks

This would require the use of DIM, which to my knowledge hasn't been used in any significant capacity within the core libraries.

The DIM implementation would either need to throw something like NotSupported_SubclassOverride, or, provide a default implementation where the Span overloads defer back to the byte overloads. For implementations that end up using the DIM, the performance characteristics would be counter-intuitive. The Spans would need to allocate arrays to use the existing implementations, where the caller might have forced the Span overloads to be called since there is some reason for folks to believe that Span = Better.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.SecurityuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions