UrlBase64 2.0.0-preview2

This is a prerelease version of UrlBase64.
There is a newer version of this package available.
See the version list below for details.
dotnet add package UrlBase64 --version 2.0.0-preview2                
NuGet\Install-Package UrlBase64 -Version 2.0.0-preview2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="UrlBase64" Version="2.0.0-preview2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add UrlBase64 --version 2.0.0-preview2                
#r "nuget: UrlBase64, 2.0.0-preview2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install UrlBase64 as a Cake Addin
#addin nuget:?package=UrlBase64&version=2.0.0-preview2&prerelease

// Install UrlBase64 as a Cake Tool
#tool nuget:?package=UrlBase64&version=2.0.0-preview2&prerelease                

UrlBase64

UrlBase64 is a package for all versions of the .NET Framework and .NET Core, which supports the creation of url-safe base64 encoding. UrlBase64 provides symmetric routines to both encode and later decode data in a fashion that makes it safe for use on the general web and as input to ASP.NET web applications (under IIS or similar).

Installation

UrlBase64 is available via nuget and the Visual Studio package manager. To install, simply run the following in the package manager console:

Install-Package UrlBase64

Or search for UrlBase64 in the Visual Studio package manager.

Basic Usage

All functions are contained in the static UrlBase64 class, found in the NeoSmart.Utils namespace.

using NeoSmart.Utils;

void UsageSample()
{
    var foo = Encoding.UTF8.GetBytes("foo");
    var encoded = UrlBase64.Encode(foo);
    var decoded = UrlBase64.Decode(encoded);

    var bar = Encoding.UTF8.GetString(decoded);

    Assert.AreEqual("foo", bar);
}

Advanced Usage

The UrlBase64 library comes with several variants of the Encode() and Decode() routines that can be used for better performance. In particular:

  • It is possible (and, assuming it works for you, preferable) to use EncodeUtf8() or one of the other Encode() variants that return a span or array of ASCII/UTF-8 bytes (being the URL-safe base64-encoded result) instead of a string for better performance and lower memory usage.
  • Similarly, it is preferred to use the Decode(byte[]) or Decode(ReadOnlySpan<byte>) overloads instead of Decode(string).
  • There are overloads of both Encode() and Decode() which can complete in an allocation-free manner. These take a second Span<byte> buffer parameter that provides the scratch space to decode into and return a Span<byte> instead of a byte[] that represents the result of the encode or decode operation. This permits you to allocate a buffer once and reuse it for all subsequent calls (or use something like ArrayPool<byte>, etc) instead of having the Encode() or Decode() operation allocate a new buffer for the result each time.
  • To ensure you are passing in a buffer of sufficient size (otherwise the Encode() or Decode() operations will throw!), use UrlBase64.GetMaxEncodedLength() or UrlBase64.GetMaxDecodedLength() to obtain an adequately sized buffer to encode/decode into.

Padding Options (Encoding)

UrlBase64 supports two different padding modes (internally: PaddingPolicy) for generating base64 content:

public enum PaddingPolicy
{
    Discard,
    Preserve,
}

The PaddingPolicy option controls the behavior of UrlBase64 when encoding content that does not fall on a 4-character (output) boundary. Per RFC 4648, base64 specifies that an "optional depending on the circumstances" trailing = sign is used to pad the output to be a multiple of 4 characters long. UrlBase64 supports both padded and unpadded output via an optional PaddingPolicy parameter to UrlBase64.Encode controlling this behavior. The default behavior at this time is to omit the trailing padding given that it a) can (and usually is) be inferred automatically when dropped from the encoded output, and b) utilizes a symbol that requires encoding when used in URLs.

var bytes = Encoding.UTF8.GetBytes("Mary had a little lamb");
Console.WriteLine(bytes, UrlBase64.Encode(bytes, PaddingPolicy.Discard));
Console.WriteLine(bytes, UrlBase64.Encode(bytes, PaddingPolicy.Preserve));

which emits the following:

TWFyeSBoYWQgYSBsaXR0bGUgbGFtYg

TWFyeSBoYWQgYSBsaXR0bGUgbGFtYg==

UrlBase64 is developed by Mahmoud Al-Qudsi of NeoSmart Technologies. UrlBase64 is released under the terms of the MIT Public License. Copyright NeoSmart Technologies 2017-2023.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.0 is compatible.  netstandard1.1 was computed.  netstandard1.2 was computed.  netstandard1.3 was computed.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net20 is compatible.  net35 was computed.  net40 is compatible.  net403 was computed.  net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Windows Phone wp8 was computed.  wp81 was computed.  wpa81 was computed. 
Windows Store netcore was computed.  netcore45 was computed.  netcore451 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (9)

Showing the top 5 NuGet packages that depend on UrlBase64:

Package Downloads
H.Necessaire.Runtime

A Core Framework for Rapid Application Development

Enmeshed.Tooling

Package Description

SingBoxLib

Configure and run sing-box with ease. Based on sing-box's official documentation.

GeometryDashAPI

API for game Geometry Dash

UwpCache

A persistent object cache for your Windows 10 uwp and uap applications.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 94,269 5/16/2024
2.0.0-preview2 114 4/18/2024
2.0.0-preview1 1,545 10/28/2023
1.0.1 150,828 9/27/2023
1.0.0 10,897 9/5/2023 1.0.0 is deprecated.
0.1.2 1,216,270 2/2/2018
0.1.1 5,674 9/21/2017
0.1.0 3,231 7/13/2017

2.0.0:
   * Add support for allocation-free encode/decode
   * Add new binary/UTF-8 Decode() method to decode url-safe base64-encoded bytes directly,
     without going through a string representation.

1.0.0:
   * Support for Span<T> and co.

0.1.2:
   * New PaddingPolicy option to specify output padding
   * Lower .NET Standard requirement to 1.0


0.1.1:
   * Fixed an encoded error with inputs of a certain length.


0.1:
   * Initial release