A high-performance .NET library for encoding and decoding base64url data as defined in RFC 4648 Section 5.
Warning
NCode.Base64Url is deprecated. Use System.Buffers.Text.Base64Url, built into the current LTS and provided by the Microsoft.Bcl.Memory package on older frameworks, and NCode.Buffers for ReadOnlySequence<T> and IBufferWriter<T>. The migration guide maps every method. Every published version stays on nuget.org and works as it does, but no version gets fixes.
NCode.Base64Url is retired, and its repository is archived: it takes no issues, pull requests or discussion (ADR-1000).
A high-performance .NET library for encoding and decoding base64url data as defined in RFC 4648 Section 5. Built with modern memory-efficient APIs including Span<T>, ReadOnlySequence<T>, and IBufferWriter<T>.
- URL-Safe Encoding — Uses
-and_instead of+and/, making encoded data safe for URLs, filenames, and identifiers - No Padding — Omits trailing
=padding characters; the decoder rejects=padding and whitespace (#39) - Zero-Allocation Options —
TryEncode/TryDecodemethods write directly to caller-provided buffers - Streaming Support — Full support for
ReadOnlySequence<T>for processing fragmented/pipelined data - Buffer Writer Integration — Direct output to
IBufferWriter<T>for high-throughput scenarios - High Performance — Optimized with unsafe code, lookup tables, and aggressive inlining
Add the package to a project:
dotnet add package NCode.Base64Urlusing NCode.Encoders;
// Encoding
byte[] data = { 0x48, 0x65, 0x6C, 0x6C, 0x6F }; // "Hello" in ASCII
string encoded = Base64Url.Encode(data); // Returns "SGVsbG8"
// Decoding
byte[] decoded = Base64Url.Decode("SGVsbG8"); // Returns original bytes| Method | Description |
|---|---|
GetCharCountForEncode(int) |
Calculates the output character count for a given byte count |
Encode(ReadOnlySpan<byte>) |
Encodes bytes to a base64url string |
Encode(ReadOnlySequence<byte>) |
Encodes a byte sequence to a base64url string |
Encode(ReadOnlySpan<byte>, IBufferWriter<char>) |
Encodes bytes directly to a buffer writer |
Encode(ReadOnlySequence<byte>, IBufferWriter<char>) |
Encodes a byte sequence directly to a buffer writer |
TryEncode(ReadOnlySpan<byte>, Span<char>, out int) |
Attempts to encode bytes to a provided character buffer |
TryEncode(ReadOnlySequence<byte>, Span<char>, out int) |
Attempts to encode a byte sequence to a provided character buffer |
| Method | Description |
|---|---|
GetByteCountForDecode(int) |
Calculates the output byte count for a given character count |
Decode(ReadOnlySpan<char>) |
Decodes a base64url string to a byte array |
Decode(ReadOnlySpan<char>, IBufferWriter<byte>) |
Decodes a base64url string directly to a buffer writer |
Decode(ReadOnlySequence<char>, IBufferWriter<byte>) |
Decodes a character sequence directly to a buffer writer |
TryDecode(ReadOnlySpan<char>, Span<byte>, out int) |
Attempts to decode a base64url string to a provided byte buffer |
TryDecode(ReadOnlySequence<char>, Span<byte>, out int) |
Attempts to decode a character sequence to a provided byte buffer |
byte[] data = GetData();
int charCount = Base64Url.GetCharCountForEncode(data.Length);
Span<char> buffer = stackalloc char[charCount];
if (Base64Url.TryEncode(data, buffer, out int charsWritten))
{
// Use buffer[..charsWritten]
}ReadOnlySpan<char> encoded = "SGVsbG8";
int byteCount = Base64Url.GetByteCountForDecode(encoded.Length);
Span<byte> buffer = stackalloc byte[byteCount];
if (Base64Url.TryDecode(encoded, buffer, out int bytesWritten))
{
// Use buffer[..bytesWritten]
}var writer = new ArrayBufferWriter<char>();
int charsWritten = Base64Url.Encode(data, writer);- .NET 8.0
- .NET 10.0
| Version | Changes |
|---|---|
| v1.0.0 | Initial release |
| v1.1.0 | Added support for ReadOnlySequence<T> |
| v1.1.1 | Added sequence overloads without buffer writer |
| v1.1.2 | Optimized single-segment sequence handling |
| v2.0.0 | Target .NET 8.0+ only |
| v2.0.1 | Updates to xmldoc and readme |
- Issues: past reports; the archived repository takes no new ones.
- CONTRIBUTING.md: how to set up, build and test the code.
- SECURITY.md: how to report a vulnerability privately.
- CODE_OF_CONDUCT.md, GOVERNANCE.md and ROADMAP.md: how we work together, who decides, and where the project is going.
- docs/architecture.md and docs/assurance-case.md: how the project is built, and why it meets its security requirements.
- docs/migration.md: how to move to
System.Buffers.Text.Base64Urland NCode.Buffers. - docs/decisions/: the decisions behind the design, the project's and the template's.
- docs/replies.md: the replies the maintainers post to people outside the project.
- CHANGELOG.md: what changed in each release, in the Keep a Changelog format.
- LICENSE.txt: the license.
Licensed under the Apache License, Version 2.0.
