Skip to content
This repository was archived by the owner on Nov 29, 2026. It is now read-only.
NCodeGroupPublic archive

About

This library provides methods for encoding and decoding base64url (RFC 4648) data using the new memory efficient Span and Buffer APIs.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

154 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

NCode Group

NCode.Base64Url

A high-performance .NET library for encoding and decoding base64url data as defined in RFC 4648 Section 5.

NuGet version NuGet downloads CI Target framework License OpenSSF Scorecard

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>.

Features

  • 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/TryDecode methods 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

Installation

Add the package to a project:

dotnet add package NCode.Base64Url

Quick start

using 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

Usage

Encoding Methods

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

Decoding Methods

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

Zero-Allocation Encoding

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]
}

Zero-Allocation Decoding

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]
}

Using with IBufferWriter

var writer = new ArrayBufferWriter<char>();
int charsWritten = Base64Url.Encode(data, writer);

Target Frameworks

  • .NET 8.0
  • .NET 10.0

Release Notes

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

Project

License

Licensed under the Apache License, Version 2.0.

About

This library provides methods for encoding and decoding base64url (RFC 4648) data using the new memory efficient Span and Buffer APIs.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages