Sqids 1.0.2

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Sqids --version 1.0.2
NuGet\Install-Package Sqids -Version 1.0.2
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="Sqids" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sqids --version 1.0.2
#r "nuget: Sqids, 1.0.2"
#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 Sqids as a Cake Addin
#addin nuget:?package=Sqids&version=1.0.2

// Install Sqids as a Cake Tool
#tool nuget:?package=Sqids&version=1.0.2

<p align="center"> <a href="https://sqids.org/dotnet"> <img alt="Logo" src="logo.svg" /> </a> </p> <h1 align="center">Sqids .NET</h1> <p align="center"> <a href="https://www.nuget.org/packages/Sqids"> <img alt="Nuget" src="https://img.shields.io/nuget/v/Sqids?style=for-the-badge&logo=nuget&color=008FF7" /> </a> <a href="https://www.nuget.org/packages/Sqids"> <img alt="Downloads" src="https://img.shields.io/nuget/dt/Sqids?style=for-the-badge" /> </a> <a href="https://github.com/sqids/sqids-dotnet/releases"> <img alt="Release" src="https://img.shields.io/github/v/release/sqids/sqids-dotnet?style=for-the-badge&color=FB0088" /> </a> <a href="https://github.com/sqids/sqids-dotnet/tree/main/src/Sqids"> <img alt="Language" src="https://img.shields.io/badge/written_in-C%23-8F00FF?style=for-the-badge" /> </a> <a href="LICENSE"> <img alt="License" src="https://img.shields.io/github/license/sqids/sqids-dotnet?style=for-the-badge&color=FFA800" /> </a> </p> <p align="center"> Sqids (pronounced "squids") is a small library that lets you generate YouTube-looking IDs from numbers. It's good for link shortening, fast & URL-safe ID generation and decoding back into numbers for quicker database lookups. </p>


Getting Started

Install the NuGet package:

Install-Package Sqids

Alternatively, using the .NET CLI:

dotnet add package Sqids

Usage:

All you need is an instance of SqidsEncoder, which is the main class, responsible for both encoding and decoding.

Using the default parameterless constructor configures SqidsEncoder with the default options:

using Sqids;
var sqids = new SqidsEncoder();
Single number:
string id = sqids.Encode(1); // "UfB"
int number = sqids.Decode(id).Single(); // 123
Multiple numbers:
string id = sqids.Encode(1, 2, 3); // "8QRLaD"
int[] numbers = sqids.Decode(id); // new[] { 1, 2, 3 }

Note Sqids also preserves the order when encoding/decoding multiple numbers.

Customizations:

You can easily customize the alphabet (the characters that Sqids uses to encode the numbers), the minimum length of the IDs (how long the IDs should be at minimum), and the blocklist (the words that should not appear in the IDs), by passing an instance of SqidsOptions to the constructor of SqidsEncoder.

You can specify all the properties, and any you leave out will fall back to their default values.

Custom Alphabet:

You can give Sqids your own custom (ideally shuffled) alphabet to use in the IDs:

var sqids = new SqidsEncoder(new()
{
    // This is a shuffled version of the default alphabet, which includes lowercase letters (a-z), uppercase letters (A-Z), and digits (0-9)
    Alphabet = "mTHivO7hx3RAbr1f586SwjNnK2lgpcUVuG09BCtekZdJ4DYFPaWoMLQEsXIqyz",
});

Note It's recommended that you at least provide a shuffled alphabet when using Sqids — even if you want to use the same characters as those the default alphabet — so that your IDs will be unique to you.

Minimum Length:

By default, Sqids uses as few characters as possible to encode a given number. However, if you want all your IDs to be at least a certain length (e.g. for aesthetic reasons), you can configure this via the MinLength option:

var sqids = new SqidsEncoder(new()
{
    MinLength = 5,
});
Custom Blocklist:

Sqids comes with a large default blocklist which will ensure that common cruse words and such never appear anywhere in your IDs. You can add extra items to this default blocklist like so:

var sqids = new SqidsEncoder(new()
{
    BlockList = { "whatever", "else", "you", "want" },
});

Note Notice how the new keyword is omitted in the snippet above (yes, this is valid C#). This way the specified strings are "added" to the default blocklist, as opposed to overriding it — which is what would happen had you done new() { ... } (which you're also free to do if that's what you want).

Advanced Usage:

Decoding a single number:

If you're decoding user-provided input and expect a single number, you can use C## pattern matching to do the necessary check and extract the number in one go:

if (sqids.Decode(input) is [var singleNumber])
{
    // you can now use `singleNumber` (which is an `int`) however you wish
}

Note This expression ensures that the decoded result is exactly one number, that is, not an empty array (which is what Decode returns when the input is invalid), and also not more than one number.

Ensuring an ID is "canonical":

Due to the design of Sqids's algorithm, decoding random IDs can sometimes produce the same numbers. For example, with the default options, both 2fs and OSc are decoded into the number 3168. This can be problematic in certain cases, such as when you're using these IDs as part of your URLs to identify resources; this way, the fact that more than one ID decodes into the same number means the same resource would be accessible with two different URLs, which is often undesirable.

The best way to mitigate this is to check if an ID is "canonical" before using its decoded value to do a database lookup, for example; and this can be done by simply re-encoding the decoded number(s) and checking if the result matches the incoming ID:

int[] numbers = sqids.Decode(input);
bool isCanonical = input == sqids.Encode(numbers); // If `input` is `OSc`, this evaluates to `true` (because that's the canonical encoding of `3168`), and if `input` is `2fs`, it evaluates to `false`.

You can combine this check with the check for a single number (the previous example) like so:

if (sqids.Decode(input) is [var id] &&
    input == sqids.Encode(id))
{
    // `input` decodes into a single number and is canonical, now you can safely use it
}
Dependency injection:

To use SqidsEncoder with a dependency injection system, simply register it as a singleton service:

With default options:

services.AddSingleton<SqidsEncoder>();

With custom options:

services.AddSingleton(new SqidsEncoder(new()
{
    Alphabet = "ABCEDFGHIJ0123456789",
    MinLength = 6,
}));

And then you can inject it anywhere you need it:

public class SomeController
{
    private readonly SqidsEncoder _sqids;
    public SomeController(SqidsEncoder sqids)
    {
        _sqids = sqids;
    }
}

License:

MIT

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on Sqids:

Package Downloads
Orbital7.Extensions The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

.NET Extensions/Utilities

gitViwe.Shared.MediatR

A .NET library for some additional MediatR implementation

laget.Sqids The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

An implementation for hashed ids using Sqids to be used in public facing DTOs

gitViwe.Shared.Sqids

An abstraction on top of Sqids, a small library that generates YouTube-like IDs from numbers.

Valtuutus.Data

Valtuutus data abstractions; Valtuutus provides a developer-focused, modern library for creating ReBAC without complexity.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.0 124,860 12/1/2023
3.0.3 55,058 9/15/2023
3.0.2 745 9/13/2023
3.0.1 292 9/10/2023
3.0.0 153 9/10/2023
2.0.0 1,972 8/30/2023
1.1.0 1,427 8/18/2023
1.0.5 135 8/16/2023
1.0.4 306 8/8/2023
1.0.3 121 8/8/2023
1.0.2 157 8/7/2023
1.0.1 146 8/7/2023
1.0.0 209 8/6/2023
1.0.0-preview.3 79 8/6/2023
1.0.0-preview.2 75 8/4/2023
1.0.0-preview.1 75 8/4/2023