Table of Contents

Base64Url Class

Definition

Namespace
Bodu.Text.Encoding
Assembly
Bodu.Text.Encoding.dll
Package
Bodu.Text.Encoding 1.0.0
Source
Base64Url.cs

Provides the RFC 4648 §5 URL- and filename-safe Base64 encoding as a first-class type, mirroring the shape of System.Buffers.Text.Base64Url introduced in .NET 9. URL-safe Base64 swaps + and / for - and _, and conventionally omits trailing = padding (matching JWT and OAuth practice).

public static class Base64Url
Inheritance
Base64Url
Inherited Members

Examples

// Encode a JWT segment - URL-safe alphabet, no padding.
byte[] header   = "{\"alg\":\"HS256\",\"typ\":\"JWT\"}"u8.ToArray();
string segment  = Base64Url.Encode(header);                                  // "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"

// Decoder accepts inputs both with and without trailing '=' padding.
byte[] roundtrip = Base64Url.Decode(segment);
byte[] padded    = Base64Url.Decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9==");

Remarks

This is a convenience wrapper over Base64 with UrlSafe pre-bound. Code that knows it wants URL-safe Base64 should prefer this type over passing the variant explicitly to every call. Both forms produce identical output.

Methods

Decode(ReadOnlySpan<byte>)

Decodes UTF-8 URL-safe Base64 bytes into a byte array.

public static byte[] Decode(ReadOnlySpan<byte> utf8Source)

Parameters

utf8Source ReadOnlySpan<byte>

The UTF-8 URL-safe Base64 source.

Returns

byte[]

The decoded byte array.

Exceptions

FormatException

Thrown when the input is not valid URL-safe Base64.

Decode(ReadOnlySpan<char>)

Decodes chars as a URL-safe Base64 character span.

public static byte[] Decode(ReadOnlySpan<char> chars)

Parameters

chars ReadOnlySpan<char>

The URL-safe Base64 character span.

Returns

byte[]

The decoded byte array.

Exceptions

FormatException

Thrown when the input is not valid URL-safe Base64.

Decode(string)

Decodes s as a URL-safe Base64 string. Accepts inputs both with and without trailing = padding (the JWT / OAuth convention is to omit it).

public static byte[] Decode(string s)

Parameters

s string

The URL-safe Base64 input.

Returns

byte[]

The decoded byte array.

Exceptions

ArgumentNullException

Thrown when s is null.

FormatException

Thrown when the input is not valid URL-safe Base64.

Encode(byte[])

Encodes bytes into a URL-safe Base64 string without padding.

public static string Encode(byte[] bytes)

Parameters

bytes byte[]

The bytes to encode.

Returns

string

The URL-safe Base64 string.

Exceptions

ArgumentNullException

Thrown when bytes is null.

Encode(ReadOnlySpan<byte>)

Encodes bytes into a URL-safe Base64 string without padding.

public static string Encode(ReadOnlySpan<byte> bytes)

Parameters

bytes ReadOnlySpan<byte>

The bytes to encode.

Returns

string

The URL-safe Base64 string.

EncodeToUtf8(ReadOnlySpan<byte>)

Encodes source into a UTF-8 byte array using the URL-safe Base64 alphabet.

public static byte[] EncodeToUtf8(ReadOnlySpan<byte> source)

Parameters

source ReadOnlySpan<byte>

The bytes to encode.

Returns

byte[]

The UTF-8 encoded URL-safe Base64 bytes.

GetEncodedLength(int)

Returns the maximum number of characters required to encode byteCount bytes in URL-safe Base64 (without padding by default).

public static int GetEncodedLength(int byteCount)

Parameters

byteCount int

The input byte count.

Returns

int

The encoded character count.

GetMaxDecodedLength(int)

Returns the maximum number of bytes that decoding charCount characters can produce.

public static int GetMaxDecodedLength(int charCount)

Parameters

charCount int

The input character count.

Returns

int

The maximum decoded byte count.

IsValid(ReadOnlySpan<char>)

Indicates whether source is a valid URL-safe Base64 input.

public static bool IsValid(ReadOnlySpan<char> source)

Parameters

source ReadOnlySpan<char>

The input characters.

Returns

bool

true when the input would decode cleanly; otherwise false.

TryDecode(ReadOnlySpan<char>, Span<byte>, out int)

Attempts to decode chars as URL-safe Base64 into destination.

public static bool TryDecode(ReadOnlySpan<char> chars, Span<byte> destination, out int bytesWritten)

Parameters

chars ReadOnlySpan<char>

The URL-safe Base64 character span.

destination Span<byte>

The destination byte span.

bytesWritten int

When this method returns, contains the number of bytes written.

Returns

bool

true on success; false on malformed input or undersized destination.

TryEncode(ReadOnlySpan<byte>, Span<char>, out int)

Attempts to encode source into destination as URL-safe Base64.

public static bool TryEncode(ReadOnlySpan<byte> source, Span<char> destination, out int charsWritten)

Parameters

source ReadOnlySpan<byte>

The bytes to encode.

destination Span<char>

The destination span.

charsWritten int

When this method returns, contains the number of characters written.

Returns

bool

true on success; false when the destination is too small.

TryEncodeToUtf8(ReadOnlySpan<byte>, Span<byte>, out int)

Attempts to encode source into destination as URL-safe Base64 UTF-8 bytes.

public static bool TryEncodeToUtf8(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)

Parameters

source ReadOnlySpan<byte>

The bytes to encode.

destination Span<byte>

The destination UTF-8 byte span.

bytesWritten int

When this method returns, contains the number of bytes written.

Returns

bool

true on success; false when the destination is too small.

Applies to

ProductVersions
.NET8, 10