Skip to content

[API Proposal]: Base64Url option to emit padding #129847

Description

@mtmk

Background and motivation

Base64Url (added in .NET 9, #66841) always emits output without '=' padding which follows the RFC 7515 (JOSE/JWT) convention. Padding question was raised in #66841 but left unresolved, and the type shipped unpadded.

RFC 4648 treats the alphabet (section 5) and padding (section 3.2) as separate choices, and section 3.2 leaves padding to the referencing spec. Padded base64url is conformant, and it is the default url-safe encoding elsewhere: Go base64.URLEncoding, Python base64.urlsafe_b64encode, and Java Base64.getUrlEncoder() all pad by default (the unpadded form is the opt-in in each: RawURLEncoding, and withoutPadding()). For SHA-256("hello") those produce "LPJNul-wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=", while Base64Url.EncodeToString produces the 43-char "LPJNul-wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ".

When a wire format uses padded base64url, there is no out-of-the-box way to produce it. The workarounds are to call Base64Url and append '=' manually, or to use Convert.ToBase64* and substitute '+'/'/' with '-'/'_'. A concrete case: the NATS object store stores integrity digests as "SHA-256=<padded base64url>" and derives subject names from padded base64url; the reference client uses Go's base64.URLEncoding, so every other client has to emit padding to interoperate. The NATS .NET client implements this by encoding standard base64 and substituting characters (nats-io/nats.net#1199), exactly the reimplementation #66841 set out to avoid.

Decoding is unaffected: DecodeFromChars/DecodeFromUtf8 already accept input with or without padding, so this is an encode-side request only. The reverse is the problem. Other implementations are not all lenient: Go base64.URLEncoding.DecodeString and Python base64.urlsafe_b64decode both error when expected padding is missing (Go: "illegal base64 data at input byte 40"; Python: "Incorrect padding"), whereas Java Base64.getUrlDecoder and .NET happen to accept either form. So a .NET producer that emits unpadded output is rejected outright by a Go or Python consumer expecting padded base64url. The receiver can't be relied on to tolerate the difference, which is the reason the producer needs a way to emit padding.

API Proposal

namespace System.Buffers.Text;

[Flags]
public enum Base64UrlOptions
{
    None = 0,
    InsertPadding = 1,
}

public static partial class Base64Url
{
    public static string EncodeToString(ReadOnlySpan<byte> source, Base64UrlOptions options);
    public static byte[] EncodeToUtf8(ReadOnlySpan<byte> source, Base64UrlOptions options);
    public static char[] EncodeToChars(ReadOnlySpan<byte> source, Base64UrlOptions options);

    public static int EncodeToUtf8(ReadOnlySpan<byte> source, Span<byte> destination, Base64UrlOptions options);
    public static int EncodeToChars(ReadOnlySpan<byte> source, Span<char> destination, Base64UrlOptions options);

    public static bool TryEncodeToUtf8(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, Base64UrlOptions options);
    public static bool TryEncodeToChars(ReadOnlySpan<byte> source, Span<char> destination, out int charsWritten, Base64UrlOptions options);
    public static bool TryEncodeToUtf8InPlace(Span<byte> buffer, int dataLength, out int bytesWritten, Base64UrlOptions options);

    public static OperationStatus EncodeToUtf8(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out int bytesWritten, Base64UrlOptions options, bool isFinalBlock = true);
    public static OperationStatus EncodeToChars(ReadOnlySpan<byte> source, Span<char> destination, out int bytesConsumed, out int charsWritten, Base64UrlOptions options, bool isFinalBlock = true);

    public static int GetEncodedLength(int bytesLength, Base64UrlOptions options);
}

Same pattern as Base64FormattingOptions on Convert.ToBase64String: a [Flags] enum with None and one bit, so the default is unchanged and padding is opt-in. On the OperationStatus overloads, padding is only emitted on the final block; with isFinalBlock: false the option has no effect on that call. Decoding is unchanged, since it already accepts both forms.

API Usage

byte[] hash = SHA256.HashData(data);
string digest = "SHA-256=" + Base64Url.EncodeToString(hash, Base64UrlOptions.InsertPadding);
// "SHA-256=LPJNul-wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ="

Alternative Designs

Leave as-is and have callers append padding or post-process Convert.ToBase64*. That works but pushes the same step onto every interop consumer, which is the duplication #66841 set out to remove.

A bool includePadding parameter instead of the enum. Fewer new types, but on the OperationStatus overloads it sits next to the existing bool isFinalBlock and a 5-argument call written as EncodeToUtf8(source, destination, out consumed, out written, true) still binds to the current overload, so true intended as includePadding is taken as isFinalBlock and the output is unpadded. That compiles and only fails at the consumer. The enum makes the two arguments non-interchangeable at the call site.

Risks

Additive. Default behavior is unchanged; padding appears only when explicitly requested.

Activity

  1. added
    api-suggestion
    on Jun 25, 2026
  2. added
    untriaged
    on Jun 25, 2026
  3. dotnet-policy-service commented on Jun 25, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-text-encoding
    See info in area-owners.md if you want to be subscribed.

  4. dotnet-policy-service commented on Jun 25, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-buffers
    See info in area-owners.md if you want to be subscribed.

  5. added
    api-ready-for-review
    and removed
    untriaged
    on Aug 17, 2026
  6. added this to the Future milestone on Aug 17, 2026
  7. lilinus commented on Aug 29, 2026

    @lilinus
    Contributor
        public static OperationStatus EncodeToUtf8(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out int bytesWritten, bool includePadding, bool isFinalBlock = true);
        public static OperationStatus EncodeToChars(ReadOnlySpan<byte> source, Span<char> destination, out int bytesConsumed, out int charsWritten, bool includePadding, bool isFinalBlock = true);

    Seems like the shape on these two could be error-prone since they would be new overloads for existing methods; would it be easy to mix up isFinalBlock and includePadding depending on how the result of overload resoution?

  8. mtmk commented on Aug 29, 2026

    @mtmk
    Author

    Agreed. Actually my mistake, the 5-argument call binds to the existing overload and argument is taken as isFinalBlock. Updated the proposal to use enum instead. Same as Base64FormattingOptions on Convert.ToBase64String. The bool is now listed under alternatives.

  9. bartonjs commented on Oct 13, 2026

    @bartonjs
    Member

    Video

    • We changed Base64UrlOptions to Base64UrlFormattingOptions for better alignment with Base64FormattingOptions
    • InsertPadding => IncludePadding (Padding is Appended, not Inserted)
    • In case we ever want to support inserting linebreaks, let's leave 1 free so the option doesn't need translation across Base64FormattingOptions and Base64UrlFormattingOptions
    namespace System.Buffers.Text;
    
    [Flags]
    public enum Base64UrlFormattingOptions
    {
        None = 0,
        // 1 is reserved for the future, if we add Linebreak.
        IncludePadding = 2,
    }
    
    public static partial class Base64Url
    {
        public static string EncodeToString(ReadOnlySpan<byte> source, Base64UrlFormattingOptions options);
        public static byte[] EncodeToUtf8(ReadOnlySpan<byte> source, Base64UrlFormattingOptions options);
        public static char[] EncodeToChars(ReadOnlySpan<byte> source, Base64UrlFormattingOptions options);
    
        public static int EncodeToUtf8(ReadOnlySpan<byte> source, Span<byte> destination, Base64UrlFormattingOptions options);
        public static int EncodeToChars(ReadOnlySpan<byte> source, Span<char> destination, Base64UrlFormattingOptions options);
    
        public static bool TryEncodeToUtf8(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, Base64UrlFormattingOptions options);
        public static bool TryEncodeToChars(ReadOnlySpan<byte> source, Span<char> destination, out int charsWritten, Base64UrlFormattingOptions options);
        public static bool TryEncodeToUtf8InPlace(Span<byte> buffer, int dataLength, out int bytesWritten, Base64UrlFormattingOptions options);
    
        public static OperationStatus EncodeToUtf8(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out int bytesWritten, Base64UrlFormattingOptions options, bool isFinalBlock = true);
        public static OperationStatus EncodeToChars(ReadOnlySpan<byte> source, Span<char> destination, out int bytesConsumed, out int charsWritten, Base64UrlFormattingOptions options, bool isFinalBlock = true);
    
        public static int GetEncodedLength(int bytesLength, Base64UrlFormattingOptions options);
    }
  10. added
    api-approved
    and removed
    api-suggestion
    api-ready-for-review
    on Oct 13, 2026
  11. mtmk commented on Oct 13, 2026

    @mtmk
    Author

    Thanks. I'll start the implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Buffers

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions