Skip to content

[API Proposal]: RSA.GetMaxOutputSize #78175

@vcsjones

Description

@vcsjones

Background and motivation

This is a proposal for the discussion in #67059. We have some helpers for {EC}DSA for getting signature sizes. This adds a complementary API for RSA. The method handles a "right" size for encryption and signing, and a "worst case" for decryption.

This is useful if you want to stackalloc or rent a buffer to hold a signature and need to know how much data you need.

Remark:

I considered what this might look like for ECDiffieHellman derived keys. It's a little easier for developers to reason about those sizes from DeriveKeyFrom{Hash,Hmac} without a helper, so I didn't propose anything there.

API Proposal

namespace System.Security.Cryptography {
    public partial class RSA {
        public int GetMaxOutputSize();
    }
}

API Usage

byte[] signatureBuffer = ArrayPool<byte>.Shared.Rent(rsa.GetMaxOutputSize());
int written = rsa.Sign(blah, blah, signatureBuffer);
ReadOnlySpan<byte> signature = signatureBuffer.AsSpan(0, written);

Alternative Designs

Do nothing. Expect that developers know (KeySize + 7) / 8 is correct.

Risks

No response

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Securityin-prThere is an active PR which will close this issue when it is merged

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions