laget.Sqids 3.0.4

Prefix Reserved
Additional Details

This version contains a critical bug when using multiple alphabets that mapped all alphabet versions to the same alphabet. This leads to all alphabet versions encoding to the same hash.

We recommend updating to version 3.1.9 of this package when using multiple alphabets.

There is a newer version of this package available.
See the version list below for details.
dotnet add package laget.Sqids --version 3.0.4                
NuGet\Install-Package laget.Sqids -Version 3.0.4                
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.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add laget.Sqids --version 3.0.4                
#r "nuget: laget.Sqids, 3.0.4"                
#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.0.4

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

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.

Default alphabet: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

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": {
        "DefaultAlphabetVersion": "xx",
        "Alphabets": {
            "xx": "yyyy..."
        }
    }
}

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 int = sqid.ToInt();
    var sqid = Sqid.FromLong(42);
    var int = sqid.ToLong();

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.