laget.Sqids 3.1.9

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

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

laget.Sqids

Note The IDs that Sqids generates are unique and always decode into the same numbers. You can also make them unique to your application (so that they're not the same as everyone else who uses Sqids) by providing a shuffled alphabet.

Simple library for using hashed ids in DTOs

Based on https://www.sqids.org/

Configuration

This example is shown using Autofac since this is the go-to IoC for us.

await Host.CreateDefaultBuilder()
    .ConfigureContainer<ContainerBuilder>((context, builder) =>
    {
        builder.RegisterSqids();
    })
    .UseServiceProviderFactory(new AutofacServiceProviderFactory())
    .Build()
    .RunAsync();

Salts for the hashes will be loaded from the config file expecting the following format

{
    /* ..., */
    "Sqids": {
        "Alphabets": {
            "x1": "1mQCPfDZ0cSKuITsAjWGpe3Rwltq4ghi8J9kVLX2oEOrNByHMUbFz5dY76nvxa",
            "x2": "mBQtPx6evZfEV9TXsh1LGO2N8ScYaHJUbMdr7iCIuKp4jynD0WgAl5zwqkRo3F",
            ...
        },
        "DefaultAlphabetVersion": "x1",
        "MinLength": 13
    }
    /* ... */
}

Where 'xx' is a 2 character version code and 'yyyy' is the alphabet (of unlimited size but must be larger than 5) used by that version.

Warning Sqids needs an alphabet that contains at least 5 unique characters.

Usage

Note You can use any integral numeric type (e.g. long, byte, short, etc.) as the type argument. int is just the most common one, but if you need to encode/decode larger numbers, for example, you could use long/ulong instead.

If you're targeting an older framework than .NET 7, SqidsEncoder only supports int, and there is no generic type parameter you need to supply, so just:

Basic usage

    var sqid = Sqid.FromInt(42);
    var id = sqid.ToInt();
    var sqid = Sqid.FromLong(42);
    var id = sqid.ToLong();

If you have multiple alphabets you can specify what alphabet to use when converting an int or long to a Sqid.

    var sqid = Sqid.FromInt(42, "a2");
    var id = sqid.ToInt();
Extensions
    var sqid = (int)1.ToSqid();
    var sqid = (int)1.ToSqid("a2");
    var sqid = (int?)1.ToSqid();
    var sqid = (int?)1.ToSqid("a2");

    var sqid = (long)1.ToSqid();
    var sqid = (long)1.ToSqid("a2");
    var sqid = (long?)1.ToSqid();
    var sqid = (long?)1.ToSqid("a2");

    var sqid = "0xR4reL0zL3Xgq8".ToSqid();

Usage in a class

    public class Dto 
    {
        public Sqid Id { get; set; }
    }

    // Serializes to { Id: "somehash" }

Example when used with the laget.Mapper nuget package

    public class ModelMapper : IMapper
    {
        [MapperMethod]
        public Dto ModelToDto(Model model) => new() { Id = model.Id.ToSqid() };

        [MapperMethod]
        public Model DtoToModel(Dto dto) => new() { Id = dto.Id.ToInt() };
    }
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 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 is compatible.  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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework 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 tizen40 was computed.  tizen60 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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.9 301 1/27/2024
3.1.8 90 1/27/2024
3.0.6 94 1/20/2024
3.0.5 197 12/6/2023
3.0.4 161 11/22/2023
3.0.3 123 11/7/2023
3.0.2 153 9/27/2023
2.0.1 150 9/11/2023